---恢复内容开始---git
软件开发中,bug就像屡见不鲜同样,有了bug就须要修复,在Git中,因为分支是如此强大,因此,每一个bug均可以经过一个临时分支来修复,修复后,合并分支,而后将临时分支删除。app
当你接到一个修复一个代号101的bug任务时,很天然地,你想建立一个分支issue-101来修复它,可是,等等,当前正在dev上进行的工做尚未提交:spa
$ git status
On branch master
Your branch is up to date with 'origin/master'.开发
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)it
new file: hello.pyio
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)ast
modified: readme.txt软件
并非你不想提交,而是工做只进行到一半,还没发提交,预计完成还需1天时间,可是,必须在两个小时以内修复该bug,怎么办?date
幸亏,Git还提供了一个stash功能,能够把当前工做现场“储藏”起来,等之后恢复现场后继续工做file
$ git stash
Saved working directory and index state WIP on dev: 45e3302 add merge
如今,用git status查看工做区,就是干净的(除非有没有被Git管理的文件),所以能够放心的建立分支来修复bug。
$ git status
On branch dev
nothing to commit, working tree clean
首先肯定要在哪一个分支上修复bug,假定须要在master分支上修复,就从master建立临时分支:
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git checkout -b issue-101
Switched to a new branch 'issue-101'
如今修复bug,须要把readme.txt里面的内容“Git is software...”改成“Git is a free software....”而后提交
$ git add readme.txt
$ git commit -m "fix bug 101"
[issue-101 27c08c6] fix bug 101
1 file changed, 1 insertion(+), 1 deletion(-)
修复完成之后,切换到master分支,并合并完成,最后删除issue-101
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ 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修复只花了五分钟,如今,是时候接着回到dev分支干活了
$ git checkout 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: 45e3302 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} (b0d65dce977ac8b90006982914b08c161363c942)
再用git stash list查看,就看不到任何stash内容了:
$ git stash list
你能够屡次stash,恢复的时候,先用git stash list查看,而后恢复指定的stash,用命令:
$git stash apply stash@{0}
摘抄自:
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137602359178794d966923e5c4134bc8bf98dfb03aea3000