软件开发中,bug就像家常便饭一样。有了bug就需要修复,在Git中,由于分支是如此的强大,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除。

当你接到一个修复一个代号101的bug的任务时,很自然地,你想创建一个分支issue-101来修复它,但是,等等,当前正在dev上进行的工作还没有提交:

  1. $ git status
  2. # On branch dev
  3. # Changes to be committed:
  4. # (use "git reset HEAD <file>..." to unstage)
  5. #
  6. # new file: hello.py
  7. #
  8. # Changes not staged for commit:
  9. # (use "git add <file>..." to update what will be committed)
  10. # (use "git checkout -- <file>..." to discard changes in working directory)
  11. #
  12. # modified: readme.txt
  13. #

并不是你不想提交,而是工作只进行到一半,还没法提交,预计完成还需1天时间。但是,必须在两个小时内修复该bug,怎么办?

幸好,Git还提供了一个stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作:

  1. $ git stash
  2. Saved working directory and index state WIP on dev: 6224937 add merge
  3. HEAD is now at 6224937 add merge

现在,用git status查看工作区,就是干净的(除非有没有被Git管理的文件),因此可以放心地创建分支来修复bug。

首先确定要在哪个分支上修复bug,假定需要在master分支上修复,就从master创建临时分支:

  1. $ git checkout master
  2. Switched to branch 'master'
  3. Your branch is ahead of 'origin/master' by 6 commits.
  4. $ git checkout -b issue-101
  5. Switched to a new branch 'issue-101'

现在修复bug,需要把“Git is free software …”改为“Git is a free software …”,然后提交:

  1. $ git add readme.txt
  2. $ git commit -m "fix bug 101"
  3. [issue-101 cc17032] fix bug 101
  4. 1 file changed, 1 insertion(+), 1 deletion(-)

修复完成后,切换到master分支,并完成合并,最后删除issue-101分支:

  1. $ git checkout master
  2. Switched to branch 'master'
  3. Your branch is ahead of 'origin/master' by 2 commits.
  4. $ git merge --no-ff -m "merged bug fix 101" issue-101
  5. Merge made by the 'recursive' strategy.
  6. readme.txt | 2 +-
  7. 1 file changed, 1 insertion(+), 1 deletion(-)
  8. $ git branch -d issue-101
  9. Deleted branch issue-101 (was cc17032).

太棒了,原计划两个小时的bug修复只花了5分钟!现在,是时候接着回到dev分支干活了!

  1. $ git checkout dev
  2. Switched to branch 'dev'
  3. $ git status
  4. # On branch dev
  5. nothing to commit (working directory clean)

工作区是干净的,刚才的工作现场存到哪去了?用git stash list命令看看:

  1. $ git stash list
  2. stash@{0}: WIP on dev: 6224937 add merge

工作现场还在,Git把stash内容存在某个地方了,但是需要恢复一下,有两个办法:

一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;

另一种方式是用git stash pop,恢复的同时把stash内容也删了:

  1. $ git stash pop
  2. # On branch dev
  3. # Changes to be committed:
  4. # (use "git reset HEAD <file>..." to unstage)
  5. #
  6. # new file: hello.py
  7. #
  8. # Changes not staged for commit:
  9. # (use "git add <file>..." to update what will be committed)
  10. # (use "git checkout -- <file>..." to discard changes in working directory)
  11. #
  12. # modified: readme.txt
  13. #
  14. Dropped refs/stash@{0} (f624f8e5f082f2df2bed8a4e09c12fd2943bdd40)

再用git stash list查看,就看不到任何stash内容了:

  1. $ git stash list

你可以多次stash,恢复的时候,先用git stash list查看,然后恢复指定的stash,用命令:

  1. $ git stash apply stash@{0}

小结

修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;

当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场。

原文:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137602359178794d966923e5c4134bc8bf98dfb03aea3000