美文网首页
git分支管理之 - Bug分支

git分支管理之 - Bug分支

作者: dev7 | 来源:发表于2018-11-23 18:06 被阅读0次

    软件开发中,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
    
    // 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
    

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

    git stash

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

    $ git stash
    //  Saved working directory and index state WIP on dev: f52c633 add merge
    

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

    创建临时分支

    回到master

    $ git checkout master    //  确定所在分支
    //  ......
    
    $ git checkout -b issue-101
    //  ......
    

    在临时分支修复bug

    现在修复bug,把“xxx”改为“xxxxx”,然后提交:

    $ git add .
    $ git commit -m "修复了xxxbug"
    //  [issue-101 4c805e2] fix bug 101
    //   1 file changed, 1 insertion(+), 1 deletion(-)
    

    修改完成切换回master,最后删除issue-101分支

    $ git checkout 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
    //  ...
    
    $ git status
    //   On branch dev
    //   nothing to commit, working tree clean
    

    此时你会发现,dev是干净的,赶紧现在可以恢复该bug前的工作现场

    恢复现场

    git stash list命令看看,工作现场“储藏”

    $ git stash list
    //  stash@{0}: WIP on dev: f52c633 add merge
    

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

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

    2、用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
    

    TIPS

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

    $ git stash apply stash@{0}
    

    小结

    遇到需要修复紧急bug的时候不用慌,按照步骤完美破解

    • 使用git stash储存工作现场
    • 切换并新增临时分支,修改bug
    • 修复完bug回到工作分支,再用git stash pop回到工作现场

    相关文章

      网友评论

          本文标题:git分支管理之 - Bug分支

          本文链接:https://www.haomeiwen.com/subject/gybrpftx.html