Git Stash紧急处理问题,需要切分支

在开发过程中,大家都遇到过bug,并且有些bug是需要紧急修复的。
当开发人员遇到这样的问题时,首先想到的是我新切一个分支,把它修复了,再合并到master上。
当时问题来了,你当前正在开发的分支上面,还有未提交的代码,你又不想把代码提交了,怎么办呢?

git提供了stash功能,把当前工作目录现场给存储起来,等修复完bug,再切回来。
比如,我当前在dev分支上,我修改了hello.py文件

wangkongming@Vostro ~/babytree/github/test_git $ git st
On branch dev
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:   hello.py

no changes added to commit (use "git add" and/or "git commit -a")

执行git stash命令后,再查看分支状态

wangkongming@Vostro ~/babytree/github/test_git $ git stash
Saved working directory and index state WIP on dev: a9c8783 *modify dev 1
HEAD is now at a9c8783 *modify dev 1
wangkongming@Vostro ~/babytree/github/test_git $ git st
On branch dev
nothing to commit, working directory clean

从master上新切分支hot_fix来处理bug

wangkongming@Vostro ~/babytree/github/test_git $ git co master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
wangkongming@Vostro ~/babytree/github/test_git $ git co -b hot_fix
Switched to a new branch 'hot_fix'

修改了test/readme.txt文件

wangkongming@Vostro ~/babytree/github/test_git $ git st
On branch hot_fix
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:   test/readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

添加到版本库,提交

wangkongming@Vostro ~/babytree/github/test_git $ git add .
wangkongming@Vostro ~/babytree/github/test_git $ git ci -m 'fix bug'
[hot_fix 7b3948b] fix bug
 1 file changed, 1 insertion(+)

切回master,合并hot_fix分支

wangkongming@Vostro ~/babytree/github/test_git $ git co master
Already on 'master'
Your branch is up-to-date with 'origin/master'.
wangkongming@Vostro ~/babytree/github/test_git $ git merge hot_fix
Updating 1ebc483..7b3948b
Fast-forward
 test/readme.txt | 1 +
 1 file changed, 1 insertion(+)

ok,到此,这个紧急的bug,已经合并到master分支上。现在可以回到dev分支上继续开发了。
stash list来查看已经存储的工作现场。

wangkongming@Vostro ~/babytree/github/test_git $ git stash list
stash@{0}: WIP on dev: a9c8783 *modify dev 1

如何恢复工作现场呢?

  • 第一种方案,用git stash apply恢复,但是恢复后,stash内容不删除,需要用git stash drop来删除
  • 第二种方案,用git stash pop,恢复的同时把stash内容也删除了。
wangkongming@Vostro ~/babytree/github/test_git $ git stash pop
On branch dev
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:   hello.py

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (d658ee3001b54567337fe7fe522e6fd813fd73ae)

总结###

修复bug时,通过新建分支,去修复bug,然后合并分支,删除分支。
当前工作没有完成,先把现场git stash存储一下,去修复bug,修复完后,在git stash pop,回到工作现场。

参考文章:Bug分支

posted @ 2016-03-18 11:59  KoMiles  阅读(3362)  评论(0编辑  收藏  举报