廖雪峰Git教程笔记(十七)Bug分支

原文:https://blog.csdn.net/abc15766228491/article/details/79211593 

先新建一个分支(方法前一节课有哦),然后。。就可以做今天的实验啦
软件开发中,bug就像家常便饭一样。有了bug就需要修复,在Git中,由于分支是如此的强大,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除。
当你接到一个修复一个代号101的bug的任务时,很自然地,你想创建一个分支issue-101来修复它,但是,等等,当前正在dev上进行的工作还没有提交:

$ git status
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:   readme

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

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

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

$ git stash
Saved working directory and index state WIP on dev: d50594a merge with no-ff

现在,用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 ahead of 'origin/master' by 8 commits.
  (use "git push" to publish your local commits)
$ git checkout -b issus-101
Switched to a new branch 'issus-101'

现在修复bug,将readme里面第一句前面加一句helloworld吧,然后,提交

$ git add readme
$ git commit -m "fix bug 101"
[issus-101 ce34493] fix bug 101
 1 file changed, 1 insertion(+)

然后,切换到master分支,然后,删除这个分支

$ git checkout master 
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 8 commits.
  (use "git push" to publish your local commits)
$ git merge --no-ff -m "merged bug fix 101" issus-101
Merge made by the 'recursive' strategy.
 readme | 1 +
 1 file changed, 1 insertion(+)
git branch -d issus-101 
Deleted branch issus-101 (was ce34493).

bug修复完啦,回到dev分支继续(这里没有合并,因此readme第一行没有helloworld)

$ git checkout dev
Switched to branch 'dev'
$ git status
# On branch dev
nothing to commit (working directory clean)

刚才的工作现场,用git stash list查看

$ git stash list
stash@{0}: WIP on dev: d50594a merge with no-ff

工作现场还在,Git把stash内容存在某个地方了,但是需要恢复一下,有两个办法:
一是:git stash apply 恢复,但是恢复之后,stash内容不删除,需要用git stash drop删除;
二是:git stash pop,恢复的同时把stash内容页删除了,再查看,就没有内容了
 

$ 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:   readme

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

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

$ git stash apply stash@{0}

小结

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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章