文章目錄
- 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
-
:展示工作區和緩存區的資訊,哪些需要add,哪些需要commit。git status
-
:展示兩個commit版本的差異。git diff
、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節點。
常見用法:
-
:回退到某個commit節點。git revert <commitId>
-
:回退n步。git revert HEAD~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鍊條。
常見用法:
-
:合并目前分支的多條commit記錄。git rebase -i HEAD~n
-
:将目前分支的基點,改為master最新commit。假設兩個分支共同祖先為git rebase <org_branch> <curr_branch>
,那麼目前分支所有的送出先放到<commitId>
下,然後更新.git/rebase
到<curr_branch>
版本,最後應用<org_branch>
下的送出。.git/rebase
-
:繼續之前的rebase編輯操作(如果你不小心中斷了)git rebase --edit-todo
-
:繼續下一步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記錄,常用的有
-
:完全使用這個commit記錄。p,pick
-
:使用commit修改,但是合并到前一個commit中。s,squash
-
:不使用commit,丢失修改和日志。d,drop
比如下面的操作是最常見的,保留第一個commit,其他全部
squash
,保留commit修改,但是作為第一個commit的内容。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TP31ENrpWTzUlaOpHOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL1QTO5IzNzMjM3AjNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
git rebase --continue
編輯新的commit消息。
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>
應用到目前分支。
常見用法:
-
:挑選多個commit内容,應用到目前分支。git cherry-pick <commitId_A> <commitId_B> <commitId_C>
-
:挑選a–>n的送出内容,應用到目前分支。不包括Agit cherry-pick <commitId_A>..<commitId_N>
-
:挑選(a-1)–>n的送出内容,應用到目前分支。這下包括A了git cherry-pick <commitId_A>~..<commitId_N>
-
:繼續未完成的操作。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
手動merge後繼續