git学习9-分支管理-Bug分支
软件开发中,bug就像家常便饭一样。有了bug就需要修复,在Git中,由于分支是如此的强大,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除。
当你接到一个修复一个代号101的bug的任务时,很自然地,你想创建一个分支issue-101
来修复它,但是,等等,当前正在dev
上进行的工作还没有提交:
2CARYS4 /e/pyc_study (master)
□□ ls
main.py README.md
2CARYS4 /e/pyc_study (master)
□□ git branch
dev
feature2
* master
2CARYS4 /e/pyc_study (master)
□□ git status
On branch master
Your branch is ahead of 'origin/master' by 5 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
2CARYS4 /e/pyc_study (master)
□□ git switch dev
Switched to branch 'dev'
2CARYS4 /e/pyc_study (dev)
□□ git status
On branch dev
nothing to commit, working tree clean
2CARYS4 /e/pyc_study (dev)
□□ vim dev.txt
2CARYS4 /e/pyc_study (dev)
□□ cat dev.txt
Git is free software...
2CARYS4 /e/pyc_study (dev)
□□ ls
dev.txt main.py README.md
2CARYS4 /e/pyc_study (dev)
□□ vim README.md
2CARYS4 /e/pyc_study (dev)
□□ cat README.md | grep "04/26"
edit this line in 04/26/21.
2CARYS4 /e/pyc_study (dev)
□□ git status
On branch dev
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
dev.txt
no changes added to commit (use "git add" and/or "git commit -a")
2CARYS4 /e/pyc_study (dev)
□□
并不是你不想提交,而是工作只进行到一半,还没法提交,预计完成还需1天时间。但是,必须在两个小时内修复该bug,怎么办?
幸好,Git还提供了一个stash
功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作:
stash 英[stæʃ]
v. 存放; 贮藏; 隐藏;
n. 一批贮藏物;
2CARYS4 /e/pyc_study (dev)
□□ git stash
Saved working directory and index state WIP on dev: 3e87d02 edit by dev 2nd
2CARYS4 /e/pyc_study (dev)
□□ git log --graph --pretty=oneline --abbrev-commit
* 3e87d02 (HEAD -> dev) edit by dev 2nd
* 848bcf1 add merge
* 4f06dec (origin/master, origin/HEAD) 0423 etc.
* bdffc59 conflict fixed
|\
| * 53a3d1f AND simple
* | d71bde7 & simple
|/
* 915ed39 branch dev modified
* 2d69ef5 modify main.py from little black
* 40e5382 from carysLaptop little black
* 1aa2f87 README.md 已在 Bitbucket 中,在线编辑过。
* a2252ec modify readme.md
* 5cd5ad8 from haierLaptop PyCharm Ver01
* 2d5f4c7 Initial commit
2CARYS4 /e/pyc_study (dev)
□□ git log
commit 3e87d02c256dac8c572c75c8029b3d2339c12dee (HEAD -> dev)
Author: carysunqd <carys@aliyun.com>
Date: Sun Apr 25 14:13:12 2021 +0800
edit by dev 2nd
commit 848bcf1093242511189ca5f5af5a5fe9451169b6
Author: carysunqd <carys@aliyun.com>
Date: Sun Apr 25 13:50:36 2021 +0800
add merge
commit 4f06dec79ca968aeb31046ef401d354eef1f0a37 (origin/master, origin/HEAD)
Author: carysunqd <carys@aliyun.com>
可见git stash
并没有新的commit。
现在,用git status
查看工作区,就是干净的(除非有没有被Git管理的文件),因此可以放心地创建分支来修复bug。
2CARYS4 /e/pyc_study (dev)
□□ git status
On branch dev
Untracked files:
(use "git add <file>..." to include in what will be committed)
dev.txt
nothing added to commit but untracked files present (use "git add" to track)
2CARYS4 /e/pyc_study (dev)
□□
首先确定要在哪个分支上修复bug,假定需要在master
分支上修复,就从master
创建临时分支:
2CARYS4 /e/pyc_study (dev)
□□ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 5 commits.
(use "git push" to publish your local commits)
2CARYS4 /e/pyc_study (master)
□□ git branch
dev
feature2
* master
2CARYS4 /e/pyc_study (master)
□□ git switch -c 'issue-101'
Switched to a new branch 'issue-101'
2CARYS4 /e/pyc_study (issue-101)
□□ git branch
dev
feature2
* issue-101
master
2CARYS4 /e/pyc_study (issue-101)
□□
现在修复bug,然后提交:
2CARYS4 /e/pyc_study (issue-101)
□□ ls
dev.txt main.py README.md
2CARYS4 /e/pyc_study (issue-101)
□□ vim README.md
2CARYS4 /e/pyc_study (issue-101)
□□ cat README.md | grep <issue
bash: issue: No such file or directory
2CARYS4 /e/pyc_study (issue-101)
□□ cat README.md | grep 04/26
edit in 04/26/21. <issue-101>
2CARYS4 /e/pyc_study (issue-101)
□□ git add README.md
2CARYS4 /e/pyc_study (issue-101)
□□ git commit -m "fix bug 101"
[issue-101 487f295] fix bug 101
1 file changed, 1 insertion(+)
2CARYS4 /e/pyc_study (issue-101)
□□
修复完成后,切换到master
分支,并完成合并,最后删除issue-101
分支:
2CARYS4 /e/pyc_study (issue-101)
□□ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 5 commits.
(use "git push" to publish your local commits)
2CARYS4 /e/pyc_study (master)
□□ git merge --no-ff -m "merged bug fix 101" issue-101
Merge made by the 'recursive' strategy.
README.md | 1 +
1 file changed, 1 insertion(+)
2CARYS4 /e/pyc_study (master)
□□ git branch -d issue-101
Deleted branch issue-101 (was 487f295).
2CARYS4 /e/pyc_study (master)
□□ git branch
dev
feature2
* master
2CARYS4 /e/pyc_study (master)
□□
太棒了,原计划两个小时的bug修复只花了5分钟!现在,是时候接着回到dev
分支干活了!
2CARYS4 /e/pyc_study (master)
□□ git switch dev
Switched to branch 'dev'
2CARYS4 /e/pyc_study (dev)
□□ git status
On branch dev
Untracked files:
(use "git add <file>..." to include in what will be committed)
dev.txt
nothing added to commit but untracked files present (use "git add" to track)
2CARYS4 /e/pyc_study (dev)
□□
工作区是干净的,刚才的工作现场存到哪去了?用git stash list
命令看看:
2CARYS4 /e/pyc_study (dev)
□□ git stash list
stash@{0}: WIP on dev: 3e87d02 edit by dev 2nd
2CARYS4 /e/pyc_study (dev)
□□
工作现场还在,Git把stash内容存在某个地方了,但是需要恢复一下,有两个办法:
一是用git stash apply
恢复,但是恢复后,stash内容并不删除,你需要用git stash drop
来删除;
另一种方式是用git stash pop
,恢复的同时把stash内容也删了:
2CARYS4 /e/pyc_study (dev)
□□ git stash pop
On branch dev
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
dev.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (d82978b1d769138ed7d0894649d1256ef42e1482)
2CARYS4 /e/pyc_study (dev)
□□ git stash list
2CARYS4 /e/pyc_study (dev)
□□ git status
On branch dev
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
dev.txt
no changes added to commit (use "git add" and/or "git commit -a")
2CARYS4 /e/pyc_study (dev)
□□
再用git stash list
查看,就看不到任何stash内容了。
2CARYS4 /e/pyc_study (dev)
□□ ls
dev.txt main.py README.md
2CARYS4 /e/pyc_study (dev)
□□ vim README.md
2CARYS4 /e/pyc_study (dev)
□□ cat README.md | grep 04/26
edit this line in 04/26/21.
2CARYS4 /e/pyc_study (dev)
□□
你可以多次stash,恢复的时候,先用git stash list
查看,然后恢复指定的stash,用命令:
$ git stash apply stash@{0}
在master分支上修复了bug后,我们要想一想,dev分支是早期从master分支分出来的,所以,这个bug其实在当前dev分支上也存在。
那怎么在dev分支上修复同样的bug?重复操作一次,提交不就行了?
有木有更简单的方法?
有!
同样的bug,要在dev上修复,我们只需要把487f295 fix bug 101
这个提交所做的修改“复制”到dev分支。注意:我们只想复制487f295 fix bug 101
这个提交所做的修改,并不是把整个master分支merge过来。
为了方便操作,Git专门提供了一个cherry-pick
命令,让我们能复制一个特定的提交到当前分支:
2CARYS4 /e/pyc_study (dev)
□□ git branch
* dev
feature2
master
2CARYS4 /e/pyc_study (dev)
□□ git cherry-pick 487f295
error: Your local changes to the following files would be overwritten by merge:
README.md
Please commit your changes or stash them before you merge.
Aborting
fatal: cherry-pick failed
2CARYS4 /e/pyc_study (dev)
□□ git add .
warning: LF will be replaced by CRLF in dev.txt.
The file will have its original line endings in your working directory
2CARYS4 /e/pyc_study (dev)
□□ git commit -m 'edit by dev'
[dev db25ad1] edit by dev
2 files changed, 4 insertions(+), 2 deletions(-)
create mode 100644 dev.txt
2CARYS4 /e/pyc_study (dev)
□□ git cherry-pick 487f295
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
error: could not apply 487f295... fix bug 101
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
2CARYS4 /e/pyc_study (dev|CHERRY-PICKING)
□□ vim README.md
...
21 ## edit by branch dev
22 this line is added by branch dev.
23 this line is added by branch dev by 2nd times.
24 <<<<<<< HEAD
25 edit this line in 04/26/21.
26 =======
27 edit in 04/26/21. <issue-101>
28 >>>>>>> 487f295 (fix bug 101)
...
2CARYS4 /e/pyc_study (dev|CHERRY-PICKING)
□□
改为:
21 ## edit by branch dev
22 this line is added by branch dev.
23 this line is added by branch dev by 2nd times.
24 edit this line in 04/26/21.
25 edit in 04/26/21. <issue-101>
cherry-pick 英[ˈtʃeri pɪk]
v. 筛选; 精选;
手工解决冲突,并提交:
□□ git status
On branch dev
You are currently cherry-picking commit 487f295.
(fix conflicts and run "git cherry-pick --continue")
(use "git cherry-pick --skip" to skip this patch)
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
2CARYS4 /e/pyc_study (dev|CHERRY-PICKING)
□□ git add .
2CARYS4 /e/pyc_study (dev|CHERRY-PICKING)
□□ git status
On branch dev
You are currently cherry-picking commit 487f295.
(all conflicts fixed: run "git cherry-pick --continue")
(use "git cherry-pick --skip" to skip this patch)
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
Changes to be committed:
modified: README.md
2CARYS4 /e/pyc_study (dev|CHERRY-PICKING)
□□ git commit -m 'fix bug by issue-101, merge dev'
[dev df0b5a7] fix bug by issue-101, merge dev
Date: Mon Apr 26 18:20:01 2021 +0800
1 file changed, 1 insertion(+)
2CARYS4 /e/pyc_study (dev)
□□ git status
On branch dev
nothing to commit, working tree clean
2CARYS4 /e/pyc_study (dev)
□□
如果没有冲突,Git自动给dev分支做了一次提交,注意这次提交的commit,如1d4b803
,它并不同于master的487f295
,因为这两个commit只是改动相同,但确实是两个不同的commit。用git cherry-pick
,我们就不需要在dev分支上手动再把修bug的过程重复一遍。
有些聪明的童鞋会想了,既然可以在master分支上修复bug后,在dev分支上可以“重放”这个修复过程,那么直接在dev分支上修复bug,然后在master分支上“重放”行不行?当然可以,不过你仍然需要git stash
命令保存现场,才能从dev分支切换到master分支。
上面这句,没弄懂廖老师的意思。
小结
修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;
当手头工作没有完成时,先把工作现场git stash
一下,然后去修复bug,修复后,再git stash pop
,回到工作现场;
在master分支上修复的bug,想要合并到当前dev分支,可以用git cherry-pick <commit>
命令,把bug提交的修改“复制”到当前分支,避免重复劳动。