Git_7_Bug分支

本Git系列内容更多是基于廖雪峰老师的Git教程的个人笔记、总结和扩展,如有错误,请多多指正。

我们将GIt运用到实际工作中,肯定会遇到修复Bug,我们来模拟一下这个过程
首先创建一个你的工作分支work,你在上面书写并提交代码

$ git checkout -b work
Switched to a new branch 'work'

现在,你完成了一个项目,新建一个txt文件,命名为work.txt,写入以下内容

1234556

这个文件模拟你的正规项目,并把它提交到work分支,并合并至主分支master

$ git add work.txt

$ git commit -m 'work.txt_v1.0'
[work f50c40d] work.txt_v1.0
 1 file changed, 1 insertion(+)
 create mode 100644 work.txt

$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 17 commits.
  (use "git push" to publish your local commits)

$ git merge work
Updating 1bf1741..f50c40d
Fast-forward
 work.txt | 1 +
 1 file changed, 1 insertion(+)

完成之后,就可以回到work分支继续工作

$ git checkout work
Switched to branch 'work'

我们开始了第2个项目的开发,创建第二个文件work2.txt,写入以下内容

45678

你正在写的时候,组长找到了你,说明了第一个项目中存在严重Bug,你多写了一个5
利用Git的优势,我们可以创建一个bug修复分支来修复bug,问题来了,此时你的第二个项目还没有写完提交,如果直接创建切换bug修复分支

$ git checkout -b flexbug
Switched to a new branch 'flexbug'

$ git status
On branch flexbug
Untracked files:
  (use "git add ..." to include in what will be committed)

        work2.txt

nothing added to commit but untracked files present (use "git add" to track)

你会发现,你未完成的work2.txt也会在flexbug分支中存在,很明显这不是我希望看到的,这时候就需要使用git stash来将工作现场储存起来,让我们切换回work分支

$ git checkout work
Switched to branch 'work'

先将work2.txt提交

$ git add work2.txt

此时使用git stash

$ git stash
Saved working directory and index state WIP on work: f50c40d work.txt_v1.0

此时查看git status就会发现工作区是干净的

$ git status
On branch work
nothing to commit, working tree clean

现在就可以放心的创建切换分支了,因为上文我们已经创建了flexbug分支,我们直接切换即可

$ git checkout bug
Switched to branch 'flexbug'

你现在可以返回git目录,会发现work2.txt已经不见了,现在就可以修复work.txt中的bug了,修改work.txt中内容

123456

提交合并分支

$ git add work.tx
$ git commit -m 'work.txt_v1.1_fb'
[flexbug 49b54b0] work.txt_V1.1_fb
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 18 commits.
  (use "git push" to publish your local commits)

$ git merge --no-ff -m 'flexBug work.txt_v1.1' flexbug
Merge made by the 'recursive' strategy.
 work.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

此时打开work.txt就可以发现bug已经被修复了

123456

现在我们就可以删除flexbug分支了

$ git branch -d flexbug
Deleted branch flexbug (was 49b54b0).

修复完bug之后,我们应该回到work分支继续工作了

$ git checkout work
Switched to branch 'work'

那之前的work2.txt去哪了?可以使用git stash list查看

$ git stash list
stash@{0}: WIP on work: f50c40d work.txt_v1.0

怎么找回之前的work2.txt呢,可以使用git stash pop

$ git stash pop
On branch work
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        new file:   work2.txt

Dropped refs/stash@{0} (6db30b984686663b28c682db7d8e73066ddcb3c7)

再打开git目录,就可以发现work2.txt已经回来了

你可能感兴趣的:(Git_7_Bug分支)