Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

第 176 章 Git - Fast Version Control System

distributed revision control system

目錄

176.1. Repositories 倉庫管理
176.1.1. initial setup
176.1.2. checkout
176.1.3. Creating and Commiting
176.1.4. Manager remote
176.1.5. Status
176.1.6. Diff
176.1.6.1. --name-only 僅顯示檔案名
176.1.7. Cloning
176.1.8. Push
176.1.9. Pull
176.1.10. fetch
176.1.11. Creating a Patch
176.1.12. reset
176.2. Manipulating branches
176.2.1. list branches
176.2.2. create branches
176.2.3. delete branches
176.2.4. switch branch
176.2.5. git-show-branch - Show branches and their commits
176.3. Sharing Repositories with others
176.3.1. Setting up a git server
176.3.2. 修改 origin
176.3.3. 刪除 origin
176.4. Submodule 子模組
176.4.1. 添加模組
176.4.2. checkout 子模組
176.4.3. 刪除子模組
176.5. Git Large File Storage
176.5.1. 安裝 LFS 支持
176.5.2. LFS lock
176.6. command
176.6.1. hash-object
176.6.2. git-add - Add file contents to the index
176.6.3. git-status - Show the working tree status
176.6.4. git-commit - Record changes to the repository
176.6.5. git-show - Show various types of objects
176.6.6. git-checkout - Checkout and switch to a branch
176.6.6.1. checkout master
176.6.6.2. checkout branch
176.6.7. git config
176.6.8. git log
176.7. git-daemon 伺服器
176.7.1. git-daemon - A really simple server for git repositories
176.7.2. git-daemon-sysvinit
176.7.3. inet.conf / xinetd 方式啟動
176.7.4. git-daemon-run
176.7.5. Testing
176.8. git config
176.8.1. core.sshCommand
176.8.2. fatal: The remote end hung up unexpectedly
176.8.3. 忽略 SSL 檢查
176.9. git-svn - Bidirectional operation between a single Subversion branch and git
176.10. .gitignore
176.11. .gitattributes
176.11.1. SVN Keywords
176.12. gitolite - SSH-based gatekeeper for git repositories
176.12.1. gitolite-admin
176.12.1.1. gitolite.conf
176.12.1.1.1. staff
176.12.1.1.2. repo
176.13. Web Tools
176.13.1. viewgit
176.14. FAQ
176.14.1. 導出最後一次修改過的檔案
176.14.2. 導出指定版本區間修改過的檔案
176.14.3. 回撤提交
176.14.4. 每個項目一個證書

homepage: http://git.or.cz/index.html

過程 176.1. Git

  1. install

    sudo apt-get install git-core
    			
  2. config

    			
    $ git-config --global user.name neo
    $ git-config --global user.email openunix@163.com
    			
    			
  3. Initializ

    $ mkdir repository
    $ cd repository/
    
    /repository$ git-init-db
    Initialized empty Git repository in .git/
    			

    to check .gitconfig file

    $ cat ~/.gitconfig
    [user]
            name = chen
            email = openunix@163.com
    			

176.1. Repositories 倉庫管理

176.1.1. initial setup

Tell git who you are:

$ git config user.name "FirstName LastName"
$ git config user.email "user@example.com"

If you have many git repositories under your current user, you can set this for all of them


$ git config --global user.name "FirstName LastName"
$ git config --global user.email "user@example.com"

If you want pretty colors, you can setup the following for branch, status, and diff commands:

$ git config --global color.branch "auto"
$ git config --global color.status "auto"
$ git config --global color.diff "auto"

Or, to turn all color options on (with git 1.5.5+), use:

$ git config --global color.ui "auto"
To enable aut-detection for number of threads to use (good for multi-CPU or multi-core computers) for packing repositories, use:


$ git config --global pack.threads "0"
To disable the rename detection limit (which is set "pretty low" according to Linus, "just to not cause problems for people who have less memory in their machines than kernel developers tend to have"), use:

$ git config --global   diff.renamelimit "0"
		

176.1.2. checkout

將 nqp-cc/src/QASTCompilerMAST.nqp 檔案 重置到 211ab0b19f25b8c81685a97540f4b1491eb17504 版本

git checkout 211ab0b19f25b8c81685a97540f4b1491eb17504 -- nqp-cc/src/QASTCompilerMAST.nqp
		

176.1.3. Creating and Commiting

$ cd (project-directory)
$ git init
$ (add some files)
$ git add .
$ git commit -m 'Initial commit'
		

176.1.4. Manager remote

remote add

		
git remote add origin git@localhost:example.git
		
		

remote show

		
git remote show
origin
		
		

remote rm

		
git remote rm origin
		
		

添加多個遠程倉庫

git remote add origin git@localhost:example.git
git remote add another https://gitcafe.com/netkiller/netkiller.gitcafe.com.git
git push origin master
git push another master
		

176.1.5. Status

		
$ git clone git://10.10.0.5/example.git
Cloning into example...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 1), reused 0 (delta 0)
Receiving objects: 100% (5/5), done.
Resolving deltas: 100% (1/1), done.

neo@neo-OptiPlex-380:~/tmp$ cd example/

neo@neo-OptiPlex-380:~/tmp/example$ git status
# On branch master
nothing to commit (working directory clean)

neo@neo-OptiPlex-380:~/tmp/example$ ls
test1  test2  test3  test4

neo@neo-OptiPlex-380:~/tmp/example$ echo hello > test1

neo@neo-OptiPlex-380:~/tmp/example$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   test1
#
no changes added to commit (use "git add" and/or "git commit -a")
		
		

176.1.6. Diff

		
neo@neo-OptiPlex-380:~/tmp/example$ git diff
diff --git a/test1 b/test1
index e69de29..ce01362 100644
--- a/test1
+++ b/test1
@@ -0,0 +1 @@
+hello
		
		

比較 nqp-cc/src/QASTCompilerMAST.nqp 檔案 當前版本與 211ab0b19f25b8c81685a97540f4b1491eb17504 版本的區別

git diff 211ab0b19f25b8c81685a97540f4b1491eb17504 -- nqp-cc/src/QASTCompilerMAST.nqp
		

176.1.6.1. --name-only 僅顯示檔案名

git diff --name-only
			

176.1.7. Cloning

		
$ git clone git://github.com/git/hello-world.git
$ cd hello-world
$ (edit files)
$ git add (files)
$ git commit -m 'Explain what I changed'
		
		

176.1.8. Push

		
$ git clone git://10.10.0.5/example.git
$ cd example
$ (edit files)
$ git add (files)
$ git commit -m 'Explain what I changed'

$ git push origin master
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git://10.10.0.5/example.git
   27f8417..b088cc3  master -> master
		
		

176.1.9. Pull

$ git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From git://10.10.0.5/example
   27f8417..b088cc3  master     -> origin/master
Updating 27f8417..b088cc3
Fast-forward
 test1 |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
		

176.1.10. fetch

$ git fetch
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From git://10.10.0.5/example
   b088cc3..7e8c17d  master     -> origin/master
		

176.1.11. Creating a Patch

		
$ git clone git://github.com/git/hello-world.git
$ cd hello-world
$ (edit files)
$ git add (files)
$ git commit -m 'Explain what I changed'
$ git format-patch origin/master
		
		

176.1.12. reset

重置到上一個版本

git log
git reset --hard HEAD^
git log
git push -f