天天看點

十分鐘捋完 GIT 指令(補充)GIT指令補充

文章目錄

  • GIT指令補充
    • 簡單指令
    • revert
    • rebase
      • rebase合并commit記錄
      • rebase修改分支基點
    • cherry-pick

GIT指令補充

之前寫了十分鐘捋完 GIT 指令,内容包括了一些常用且重要的指令。但是不夠完整,今天再補充一些指令。

簡單指令

說明:首先把一些簡單的展示指令羅列一下,這些指令基本都是看一遍就會的。

  • git remote

    :管理遠端倉庫資訊,以及路徑。增加

    git remote add

    、删除

    git remote remove

    、修改

    git remote set-xxx

  • git branch

    :管理分支。檢視

    git branch -av

    、删除

    git branch -D

    、修改

    git branch -m

    、關聯遠端分支

    git branch --set-upstream

  • git tag

    :管理标簽。檢視

    git tag -l

    、新增

    git tag <tagname>

    、覆寫

    git tag -f <tagname>

    、删除

    git tag -d

  • git status

    :展示工作區和緩存區的資訊,哪些需要add,哪些需要commit。
  • git diff

    :展示兩個commit版本的差異。

    git diff <commitId>

    git diff <commitId1> <commitId2>

    git diff HEAD HEAD~

  • git log

    :展示檔案送出曆史。

    git log

    git log -p <file>

    git log --oneline

  • git blame

    :展示檔案内容的曆史。

    git blame <filename>

revert

說明:回退commit節點的修改内容,并生成一個新的commit節點。

常見用法:

  • git revert <commitId>

    :回退到某個commit節點。
  • git revert HEAD~n

    :回退n步。

示例:

# 建立環境
git checkout master && rm * && touch 1111 && git add . && git commit -m "step1" && git push
# 做兩個commit,新增2222檔案、新增3333檔案
touch 2222 && git add . && git commit -m "step2" && touch 3333 && git add . && git commit -m "step3" && git log -3
# 復原 step2,可以看到2222檔案消失、3333保留、多了一個commit曆史"revert step2"
git revert HEAD~ && git log -4
# 復原剛才的復原記錄,可以看到"revert revert step2",檔案又變成了1111、2222、3333
git revert HEAD && git log -5
# 重做兩個commit,新增4444,内容修改為5555
touch 4444 && touch 4422 && git add . && git commit -m "step4" && echo 5555 >>4444 && git add . && git commit -m "step5"
# 回退step4,4422被删除,4444提示不能還原,要求手動處理
git revert HEAD~
           

rebase

說明:整理commit曆史。可以簡化複雜的commit鍊條,由于會丢失很多commit曆史,是以慎用。一般也僅僅在自己的本地開發分支上使用,不要整理别人的commit鍊條。

常見用法:

  • git rebase -i HEAD~n

    :合并目前分支的多條commit記錄。
  • git rebase <org_branch> <curr_branch>

    :将目前分支的基點,改為master最新commit。假設兩個分支共同祖先為

    <commitId>

    ,那麼目前分支所有的送出先放到

    .git/rebase

    下,然後更新

    <curr_branch>

    <org_branch>

    版本,最後應用

    .git/rebase

    下的送出。
  • git rebase --edit-todo

    :繼續之前的rebase編輯操作(如果你不小心中斷了)
  • git rebase --continue

    :繼續下一步

rebase合并commit記錄

# 建立環境,4個commit,每個commit對應一個檔案
git checkout master && rm * &&\
touch 1111 && git add . && git commit -m "1111" &&\
touch 2222 && git add . && git commit -m "2222" &&\
touch 3333 && git add . && git commit -m "3333" &&\
touch 4444 && git add . && git commit -m "4444" && ls && git log
# 合并commit到最早的1111送出版本
git rebase -i HEAD~4
           

選擇處理commit記錄,常用的有

  • p,pick

    :完全使用這個commit記錄。
  • s,squash

    :使用commit修改,但是合并到前一個commit中。
  • d,drop

    :不使用commit,丢失修改和日志。

比如下面的操作是最常見的,保留第一個commit,其他全部

squash

,保留commit修改,但是作為第一個commit的内容。

十分鐘捋完 GIT 指令(補充)GIT指令補充

git rebase --continue

編輯新的commit消息。

十分鐘捋完 GIT 指令(補充)GIT指令補充

rebase修改分支基點

# 建立環境,master分支log:step1 step2 step3;testrebase分支log:step1 step1_1
git checkout master && rm * &&\
touch 1111 && git add . && git commit -m "step1" &&\
git checkout -B testrebase && \
echo testrebase >> 1111 && git add . && git commit -m "step1_1" && git checkout master &&\
touch 2222 && git add . && git commit -m "step2" &&\
touch 3333 && git add . && git commit -m "step3"
# 在master分支執行rebase,那麼引入step1_1送出記錄,重新生成step2 step3的送出記錄(因為1111檔案被修改了)
git rebase testrebase && cat 1111
# 也可以在testrebase分支上執行rebase,将插入step2 step3的日志,重新生成step1_1(因為新增了2222 3333兩個檔案)
git rebase master && ls
           

cherry-pick

說明:将指定的

<commitId>

應用到目前分支。

常見用法:

  • git cherry-pick <commitId_A> <commitId_B> <commitId_C>

    :挑選多個commit内容,應用到目前分支。
  • git cherry-pick <commitId_A>..<commitId_N>

    :挑選a–>n的送出内容,應用到目前分支。不包括A
  • git cherry-pick <commitId_A>~..<commitId_N>

    :挑選(a-1)–>n的送出内容,應用到目前分支。這下包括A了
  • git cherry-pick --continue

    :繼續未完成的操作。

示例:

# 創造環境,
git checkout master && rm * &&\
touch 1111 && git add . && git commit -m "step11" &&\
git checkout -B cherry1 && echo cherry1 >> 1111 && git add . && git commit -m "cherry11" &&\
git checkout master && touch 2222 && git add . && git commit -m "step22" &&\
git checkout -B cherry2 && echo cherry2 >> 2222 && git add . && git commit -m "cherry22"
# 記錄<commitId> b4f8e224  e1033d75
git checkout cherry1 && git log -1
git checkout cherry2 && git log -1
# cherry-pick
git checkout master && git cherry-pick 39124e 255d46
# 驗證,在原本的<commitId>後面新增了兩個commit曆史,同時資料也merge了。
cat 1111 && cat 2222 && git log

#### 假如 master又修改了 1111 2222
echo abcd > 1111 && echo "hello\ncherry2\nhello">2222 && git add . && git commit -m "step33"
# 這一步會提示"error: 不能應用 <commitId>... <commit msg>",
git checkout master && git cherry-pick 39124e 255d46
# 檢視哪些檔案需要合并
git status
#手動merge後繼續
git cherry-pick --continue
           
十分鐘捋完 GIT 指令(補充)GIT指令補充
十分鐘捋完 GIT 指令(補充)GIT指令補充

手動merge後繼續

十分鐘捋完 GIT 指令(補充)GIT指令補充
十分鐘捋完 GIT 指令(補充)GIT指令補充