申明:资料来源于网络及书本,通过理解、实践、整理成学习笔记。
前提
主分支:master
开发分支:develop
功能分支:feature
修补bug分支:issue-101
开发人员在 “ feature ” 分支开发的过程当中,需要中断开发并修复 “master” 分支中的 BUG,修改完成后回到 “ feature ” 分支继续开发工作。“ issue-101 ” 来源于 “ master ” 。
feature、issue-101 这种分支都属于临时性需要,使用完以后必须删除,使得代码库的常设分支始终只有Master和Develop。
一、BUG修复
假设现有工作在 “feature2 ” 分支
第1步:查看当前工作区有什么没有提交的工作:git status
第2步:把当前工作现场“储藏”起来:git stash
第3步:假定需要在 “ master ” 分支上修复BUG:git checkout master
第4步:切换分支前必查看状态:git status
第5步:创建并进入BUG修复分支:git checkout -b issue-101
第6步:修复BUG:git add README.md
第7步:提交BUG:git commit -m “fix bug 101” <----307ca0e
第8步:切换分支前必查看状态:git status
第9步:切换到master分支: git checkout master
第10步:完成合并:git merge --no-ff -m “merged bug fix 101” issue-101
第11步:最后删除issue-101分支:git branch -d issue-101
第12步:切换分支前必查看状态:git status
第13步:回到 “ feature2 ” 分支干活:git checkout feature2
第14步:在 “ feature2 ” 分支上修复相同的BUG(如果有):git cherry-pick 307ca0e
第15步:查看工作现场:git status
第16步:查看“储藏”记录:git stash list
第17步:恢复工作现场:git stash pop
第18步:解决冲突
在master分支上修复的bug,想要合并到当前 “ feature ” dev分支,可以用git cherry-pick 命令,把bug提交的修改“复制”到当前分支,避免重复劳动。
Administrator@PC202008061317 MINGW64 /c/www/git-demo (feature2)
$ git status
On branch feature2
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.md
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@PC202008061317 MINGW64 /c/www/git-demo (feature2)
$ cat README.md
这是一个测试案例
这里完成了第一个功能
第二个功能进行中。。。
Administrator@PC202008061317 MINGW64 /c/www/git-demo (feature2)
$ git stash
Saved working directory and index state WIP on feature2: ca1008f 第一个功能完成合并到 develop
Administrator@PC202008061317 MINGW64 /c/www/git-demo (feature2)
$ cat README.md
这是一个测试案例
这里完成了第一个功能
Administrator@PC202008061317 MINGW64 /c/www/git-demo (feature2)
$ git status
On branch feature2
nothing to commit, working tree clean
Administrator@PC202008061317 MINGW64 /c/www/git-demo (feature2)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Administrator@PC202008061317 MINGW64 /c/www/git-demo (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Administrator@PC202008061317 MINGW64 /c/www/git-demo (master)
$ git checkout -b issue-101
Switched to a new branch 'issue-101'
Administrator@PC202008061317 MINGW64 /c/www/git-demo (issue-101)
$ cat README.md
这是一个测试案例