Git 使用教程(六):Bug分支和Feature分支
2018-03-23 17:02
169 查看
Git 使用教程(六):Bug分支和Feature分支
一、Bug分支
软件开发中,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: hello.py
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: 4d8882e creat hello.py
再用
git status来看一下:$ git status
On b
4000
ranch dev
nothing to commit, working tree clean
工作区很干净的,因此可以放心地创建分支来修复bug。
首先确定要在哪个分支上修复bug,假定需要在
master分支上修复,就从
master创建临时分支:
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 4 commits.
$ git checkout -b issue-101
Switched to a new branch 'issue-101'
现在修复bug,需要把“I love work”改为“I love money”,然后提交:$ git add readme.txt
$ git commit -m "fix bug 101"
[issue-101 a8b5c5e] fix bug 101
1 file changed, 1 insertion(+), 1 deletion(-)修复完成后,切换到
master分支,并完成合并,最后删除
issue-101分支:$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 4 commits.
$ 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(-)
$ git branch -d issue-101 Deleted branch issue-101 (was a8b5c5e).现在,是时候接着回到
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: 4d8882e creat hello.py
工作现场还在,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: hello.py
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (acde250f8065adf2fe6484dd470c0ee8a73b8f4c)
再用
git stash list查看,就看不到任何stash内容了:$ git stash list
你可以多次stash,恢复的时候,先用
git stash list查看,然后恢复指定的stash,用命令:$ git stash apply stash@{0}
二、Feature分支
软件开发中,总有无穷无尽的新的功能要不断添加进来。添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合并,最后,删除该feature分支。现在,你终于接到了一个新任务:开发代号为Vulcan的新功能
于是准备开发:$ git checkout -b feature-vulcan
Switched to a new branch 'feature-vulcan'
$ touch vulcan.py开发完毕:$ git add vulcan.py
$ git status
On branch feature-vulcan
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: vulcan.py
$ git commit -m "add vulcan"
[feature-vulcan 83b8eba] add vulcan
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 vulcan.py
切回
dev,准备合并:$ git checkout dev一切顺利的话,feature分支和bug分支是类似的,合并,然后删除。但是,就在此时,接到上级命令,因经费不足,新功能必须取消!虽然白干了,但是这个分支还是必须就地销毁:$ git branch -d feature-vulcan
error: The branch 'feature-vulcan' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-vulcan'.
销毁失败。Git友情提醒,
feature-vulcan分支还没有被合并,如果删除,将丢失掉修改,如果要强行删除,需要使用命令
git branch -D feature-vulcan。现在我们强行删除:$ git branch -D feature-vulcan
Deleted branch feature-vulcan (was 83b8eba).
终于删除成功!
相关文章推荐
- git教程4(分支管理)创建与合并分支、Bug分支、Feature分支——廖雪峰老师
- Git使用教程(二)——Git的分支与合并
- GIT 分支管理:分支管理策略、Bug分支、Feature分支
- Git的使用十五(Feature分支)仅限Mac亲测
- git使用教程三 本地分支操作
- Git教程--Bug分支
- git使用教程三 本地分支操作
- 使用Git分支开发新特性或修复Bug与使用Git分支开发新特性或修复Bug
- Android studio使用git切换分支开发的方法(图文教程)
- Git初体验(6)-分支的冲突、管理、Bug、feature和多人协作
- 廖雪峰Git教程笔记(十八)Feature分支
- Git使用手册/Git教程:git branch 分支操作、创建分支、查看分支、删除分支、删除本地分支、删除远程分支
- git分支及多人协助使用教程及视频演示
- 优雅的使用git搭建项目环境教程--轻松拉取、合并protected的分支
- Git学习系列(七)Bug和Feature分支管理详解
- sourcetree使用教程 分支 git flow
- 五、git学习之——分支管理策略、Bug分支、feature分支、多人协作
- Git教程:Feature分支
- Git学习教程:[3]如何使用分支
- Git教程:Bug分支