軟件開發(fā)中,bug 就像家常便飯一樣。有了bug就需要修復(fù),在 Git 中,由于分支是如此的強(qiáng)大,所以,每個(gè) bug 都可以通過一個(gè)新的臨時(shí)分支來修復(fù),修復(fù)后,合并分支,然后將臨時(shí)分支刪除。
當(dāng)你接到一個(gè)修復(fù)一個(gè)代號(hào)101的bug的任務(wù)時(shí),很自然地,你想創(chuàng)建一個(gè)分支 issue-101 來修復(fù)它,但是,等等,當(dāng)前正在 dev 上進(jìn)行的工作還沒有提交:
$ git status
# 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
#
并不是你不想提交,而是工作只進(jìn)行到一半,還沒法提交,預(yù)計(jì)完成還需1天時(shí)間。但是,必須在兩個(gè)小時(shí)內(nèi)修復(fù)該 bug,怎么辦?
幸好,Git 還提供了一個(gè) stash 功能,可以把當(dāng)前工作現(xiàn)場(chǎng)“儲(chǔ)藏”起來,等以后恢復(fù)現(xiàn)場(chǎng)后繼續(xù)工作:
$ git stash
Saved working directory and index state WIP on dev: 6224937 add merge
HEAD is now at 6224937 add merge
現(xiàn)在,用git status
查看工作區(qū),就是干凈的(除非有沒有被 Git 管理的文件),因此可以放心地創(chuàng)建分支來修復(fù) bug。
首先確定要在哪個(gè)分支上修復(fù) bug,假定需要在 master 分支上修復(fù),就從 master 創(chuàng)建臨時(shí)分支:
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
$ git checkout -b issue-101
Switched to a new branch 'issue-101'
現(xiàn)在修復(fù) bug,需要把“Git is free software ...”改為“Git is a free software ...”,然后提交:
$ git add readme.txt
$ git commit -m "fix bug 101"
[issue-101 cc17032] fix bug 101
1 file changed, 1 insertion(+), 1 deletion(-)
修復(fù)完成后,切換到 master 分支,并完成合并,最后刪除 issue-101 分支:
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 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(-)
$ git branch -d issue-101
Deleted branch issue-101 (was cc17032).
太棒了,原計(jì)劃兩個(gè)小時(shí)的 bug 修復(fù)只花了 5 分鐘!現(xiàn)在,是時(shí)候接著回到 dev 分支干活了!
$ git checkout dev
Switched to branch 'dev'
$ git status
# On branch dev
nothing to commit (working directory clean)
工作區(qū)是干凈的,剛才的工作現(xiàn)場(chǎng)存到哪去了?用git stash list
命令看看:
$ git stash list
stash@{0}: WIP on dev: 6224937 add merge
工作現(xiàn)場(chǎng)還在,Git 把 stash 內(nèi)容存在某個(gè)地方了,但是需要恢復(fù)一下,有兩個(gè)辦法:
一是用git stash apply
恢復(fù),但是恢復(fù)后,stash 內(nèi)容并不刪除,你需要用git stash drop
來刪除;
另一種方式是用git stash pop
,恢復(fù)的同時(shí)把 stash 內(nèi)容也刪了:
$ 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} (f624f8e5f082f2df2bed8a4e09c12fd2943bdd40)
再用git stash list
查看,就看不到任何 stash 內(nèi)容了:
$ git stash list
你可以多次 stash,恢復(fù)的時(shí)候,先用git stash list
查看,然后恢復(fù)指定的 stash,用命令:
$ git stash apply stash@{0}
http://wiki.jikexueyuan.com/project/git-tutorial/images/stash-fix-bug.gif" alt="" />
修復(fù) bug 時(shí),我們會(huì)通過創(chuàng)建新的 bug 分支進(jìn)行修復(fù),然后合并,最后刪除;
當(dāng)手頭工作沒有完成時(shí),先把工作現(xiàn)場(chǎng)git stash
一下,然后去修復(fù) bug,修復(fù)后,再git stash pop
,回到工作現(xiàn)場(chǎng)。