Git 16. Bug分支
軟體開發中,bug就像家常便飯一樣。有了bug就需要修復,在Git中,由於分支是如此的強大,所以,每個bug都可以通過一個新的臨時分支來修復,修復後,合併分支,然後將臨時分支刪除。
當你接到一個修復一個代號101的bug的任務時,很自然地,你想建立一個分支issue-101來修復它,但是,等等,當前正在dev上進行的工作還沒有提交:
$ git status On branch dev Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: hello.py Changesnot staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: readme.txt
並不是你不想提交,而是工作只進行到一半,還沒法提交,預計完成還需1天時間。但是,必須在兩個小時內修復該bug,怎麼辦?
幸好,Git還提供了一個stash功能,可以把當前工作現場“儲藏”起來,等以後恢復現場後繼續工作:
$ git stash Saved working directory and index state WIP on dev: f52c633 add merge
現在,用git status檢視工作區,就是乾淨的(除非有沒有被Git管理的檔案),因此可以放心地建立分支來修復bug。
首先確定要在哪個分支上修復bug,假定需要在master分支上修復,就從master建立臨時分支:
$ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 6 commits. (use"git push" to publish your local commits) $ git checkout -b issue-101 Switched to a new branch 'issue-101'
現在修復bug,需要把“Git is free software ...”改為“Git is a free software ...”,然後提交:
$ git add readme.txt $ git commit -m "fix bug 101" [issue-101 4c805e2] fix bug 101 1 file changed, 1 insertion(+), 1 deletion(-)
修復完成後,切換到master分支,並完成合並,最後刪除issue-101分支:
$ git switch master Switched to branch 'master' Your branch is ahead of 'origin/master' by 6 commits. (use "git push" to publish your local commits) $ git merge --no-ff -m "merged bug fix 101" issue-101 Merge made by the 'recursive' strategy. readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
太棒了,原計劃兩個小時的bug修復只花了5分鐘!現在,是時候接著回到dev分支幹活了!
$ git switch dev Switched to branch 'dev' $ git status On branch dev nothing to commit, working tree clean
工作區是乾淨的,剛才的工作現場存到哪去了?用git stash list命令看看:
$ git stash list stash@{0}: WIP on dev: f52c633 add merge
工作現場還在,Git把stash內容存在某個地方了,但是需要恢復一下,有兩個辦法:
一是用git stash apply恢復,但是恢復後,stash內容並不刪除,你需要用git stash drop來刪除;
另一種方式是用git stash pop,恢復的同時把stash內容也刪了:
$ git stash pop On branch dev Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: hello.py 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: readme.txt Dropped refs/stash@{0} (5d677e2ee266f39ea296182fb2354265b91b3b2a)
再用git stash list檢視,就看不到任何stash內容了:
$ git stash list
你可以多次stash,恢復的時候,先用git stash list檢視,然後恢復指定的stash,用命令:
$ git stash apply stash@{0}
在master分支上修復了bug後,我們要想一想,dev分支是早期從master分支分出來的,所以,這個bug其實在當前dev分支上也存在。
那怎麼在dev分支上修復同樣的bug?重複操作一次,提交不就行了?
有木有更簡單的方法?
有!
同樣的bug,要在dev上修復,我們只需要把4c805e2 fix bug 101這個提交所做的修改“複製”到dev分支。注意:我們只想複製4c805e2 fix bug 101這個提交所做的修改,並不是把整個master分支merge過來。
為了方便操作,Git專門提供了一個cherry-pick命令,讓我們能複製一個特定的提交到當前分支:
$ git branch * dev master $ git cherry-pick 4c805e2 [master 1d4b803] fix bug 101 1 file changed, 1 insertion(+), 1 deletion(-)
Git自動給dev分支做了一次提交,注意這次提交的commit是1d4b803,它並不同於master的4c805e2,因為這兩個commit只是改動相同,但確實是兩個不同的commit。用git cherry-pick,我們就不需要在dev分支上手動再把修bug的過程重複一遍。
有些聰明的童鞋會想了,既然可以在master分支上修復bug後,在dev分支上可以“重放”這個修復過程,那麼直接在dev分支上修復bug,然後在master分支上“重放”行不行?當然可以,不過你仍然需要git stash命令儲存現場,才能從dev分支切換到master分支。
小結
修復bug時,我們會通過建立新的bug分支進行修復,然後合併,最後刪除;
當手頭工作沒有完成時,先把工作現場git stash一下,然後去修復bug,修復後,再git stash pop,回到工作現場;
在master分支上修復的bug,想要合併到當前dev分支,可以用git cherry-pick <commit>命令,把bug提交的修改“複製”到當前分支,避免重複勞動。
原文:https://www.liaoxuefeng.com/wiki/896043488029600/900388704535136