git命令使用總結

一. 分支
$ git branch //查看本地分支

* master
$ git branch -r //查看遠程分支
 origin/HEAD -> origin/removal
 origin/master
 origin/removal
$ git checkout -b  removal  origin/removal //新建本地分支並切換到遠程分支上

$git branch -d //刪除分支

二. 克隆
$ git clone ~/workspace/study/tmp.git //克隆本地倉庫

三. 補丁
$ git format-patch -1 de4f9255d //生成補丁,參數分別代表生成幾個補丁和commit ID
$ git apply --check //檢查patch能否被打上
$ git apply 001.patch
$ git am 001.patch //apply & mail

四. Rebase
1. 創建倉庫:$ git --bare init tmp.git
$ ls tmp.git
branches config description gitk.cache HEAD hooks info objects refs
2. 克隆這個倉庫:
clone-1$ git clone ~/workspace/study/tmp.git
clone-1
$ ls
tmp
3. 再克隆一個:
clone-2$ git clone ~/workspace/study/tmp.git
clone-2
$ ls
tmp
4. 修改clone-1中的文件MAINTAINERS,並commit,再push origin master提交到遠程master分支
5.
修改clone-2中的文件MAINTAINERS,並commit,此時push會失敗,需進行rebase操作後再push,如下:
$ git fetch origin
$ git rebase origin/master

$ git push origin 
//upstream branch可不填,默認是和本地分支對應的遠程分支
$ gitk //圖形化查看git log

圖1-1


看上圖1-1,通過對比紅線以上和以下的歷史信息可充分看出rebase和git pull(merge)的區別, clone…… 的信息,是clone-1分支上的提交,clone2……是clone-2上的提交,無論哪個分支先push進origin master,後面的提交者,先rebase origin/master,然後再push,就形成了紅線上方的提交歷史,很清晰,只基於一條主線。

6. 注意事項:
修改統一個文件,如果修改的是統一個地方,rebase會conflict,即便你覺得沒有修改同一行,但是修改的上下文有重疊也會被認爲衝突。

五. git log / gitk
$ git log --author="somebody" //查看作者名爲somebody的提交歷史
$ git log <文件名>/<目錄名> //查看某個文件/目錄的提交歷史

六. git diff

      $ git diff           (1)
      $ git diff --cached  (2)
      $ git diff HEAD      (3)

1. Changes in the working tree not yet staged for the next commit.
   顯示當前目錄下修改的但還沒有緩存(git add添加到Index中)的文件.
2. Changes between the index and your last commit; what you would be committing if you run "git commit" without "-a" option.
  當前Index中的和上一次commit之間的修改; 也就是你下一次commit將會提交的內容,假設你不在git commit加-a選項(-a表示自動commit所有修改或者刪除的文件)
 3. Changes in the working tree since your last commit; what you would be committing if you run "git commit -a"
    
從上一次commit開始,當前目錄下的修改(不管修改的文件有沒有被緩存,都會列出來)
 
      
 Comparing with arbitrary commits——比較任意的commit之間的修改
              $ git diff test           (1)
              $ git diff HEAD -- ./test (2)
              $ git diff HEAD^ HEAD     (3)

1. Instead of using the tip of the current branch, compare with the tip of "test" branch.
   不與當前branch進行比較,而是與test branch進行比較
2. Instead of comparing with the tip of "test" branch, compare with the tip of the current branch, but limit the comparison to the file "test".
   比較當前branch的上一個commit中的./test文件和當前目錄下的./test文件的修改
3. 比較上一個commit和上上一個commit之間的修改.

      Comparing branches——branch之間比較

              $ git diff topic master   (1)
              $ git diff topic..master  (2)
              $ git diff topic...master (3)

1. Changes between the tips of the topic and the master branches.
   比較topic 分支和master分支之間的不同
2. Same as above.
   同上。
3. Changes that occurred on the master branch since when the topic branch was started off it.
   比較topic分支從master分出來之後,topic上有哪些修改

更多git diff例子,參照git diff --help

七. git rm --cache //只從index中刪除,不刪除本地文件
八. git reset HEAD^ 回退單獨的文件,如果已經提交commit,但是想在這個commit中去掉某個文件,可以單獨回退這個文件
九.回退後的文件,用git status 查看,顯示爲已經更改,沒有add到index中,如果想撤銷更改,可以:
git checkout


九. git rebase HEAD^ --onto v3.18-rc1

將 HEAD^ rebase到 v3.18-rc1,HEAD^後面的commit會基於v3.18-rc1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章