git学习7-分支管理-解决冲突
git学习7-分支管理-解决冲突
人生不如意事常八九,合并分支往往也不是一帆风顺的。
写在开始本节学习之前
在开始本节学习之前,笔者需要清理本地操作,以便按廖老师的课程继续学习。在清理过程中发现:
- Git不允许删除当前分支(比如feature1),需切换当前分支至master,然后才允许删除feature1分支;
- 切换分支后,Git会自动提示我们当前
master
分支比远程的master
分支要超前1个提交。如Your branch is ahead of 'origin/master' by 1 commit.
21CARYC34 /e/pyc_study (feature1)
□□ git branch
* feature1
master
21CARYC34 /e/pyc_study (feature1)
□□ git branch -d feature1
error: Cannot delete branch 'feature1' checked out at 'E:/pyc_study'
21CARYC34 /e/pyc_study (feature1)
□□ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
21CARYC34 /e/pyc_study (master)
□□ git branch -d feature1
Deleted branch feature1 (was 915ed39).
21CARYC34 /e/pyc_study (master)
□□ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 406 bytes | 203.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To bitbucket.org:carysunqd/pyc_study.git
2d69ef5..915ed39 master -> master
21CARYC34 /e/pyc_study (master)
□□
从代码可以看得出,在下用上了cmder的bash来操作Git。实践出真知。
- 感觉cmder bash与Git bash,也没啥差别,也可能目前没有发现。
- 使用cmder的cmd操作Git当然也可以。但碍于cmd的vim是windows的,而非cmder的,当使用
vim <file>
打开当前目录下文件时,需要绝对路径,比较麻烦。当然可以通过windows里PATH环境变量的改变来解决,但那样又会导致Windows里gvim的打开问题,不折腾得好。 - 总体感觉cmder bash与Git bash更配套。
制造冲突(Conflict)
好了,现在转入正题,准备新的feature1
分支,继续我们的新分支开发:
21CARYC34 /e/pyc_study (master)
□□ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
21CARYC34 /e/pyc_study (master)
□□ git branch
* master
21CARYC34 /e/pyc_study (master)
□□ git switch -c feature1
Switched to a new branch 'feature1'
21CARYC34 /e/pyc_study (feature1)
□□ git branch
* feature1
master
21CARYC34 /e/pyc_study (feature1)
□□ vim README.md
修改README.md
最后一行,改为:
Creating a new branch is quick AND simple.
在feature1
分支上提交:
21CARYC34 /e/pyc_study (feature1)
□□ git add README.md
21CARYC34 /e/pyc_study (feature1)
□□ git status
On branch feature1
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
21CARYC34 /e/pyc_study (feature1)
□□ git commit -m "AND simple"
[feature1 53a3d1f] AND simple
1 file changed, 6 insertions(+), 1 deletion(-)
21CARYC34 /e/pyc_study (feature1)
□□
切换到master
分支:
21CARYC34 /e/pyc_study (feature1)
□□ git switch master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
21CARYC34 /e/pyc_study (master)
□□ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
21CARYC34 /e/pyc_study (master)
□□ vim README.md
在master
分支上把README.md
文件的最后一行改为:
Creating a new branch is quick & simple.
提交:
21CARYC34 /e/pyc_study (master)
□□ git status
On branch master
Your branch is up to date with 'origin/master'.
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
no changes added to commit (use "git add" and/or "git commit -a")
21CARYC34 /e/pyc_study (master)
□□ git add README.md
21CARYC34 /e/pyc_study (master)
□□ git commit -m "& simple"
[master d71bde7] & simple
1 file changed, 3 insertions(+), 1 deletion(-)
21CARYC34 /e/pyc_study (master)
□□
现在,master
分支和feature1
分支各自都分别有新的提交,变成了这样:
合并(Merge),发生冲突(Conflict)
这种情况下,Git无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,我们试试看:
21CARYC34 /e/pyc_study (master)
□□ git merge feature1
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
21CARYC34 /e/pyc_study (master|MERGING)
□□
果然冲突了!Git告诉我们,README.md
文件存在冲突,必须手动解决冲突后再提交。git status
也可以告诉我们冲突的文件:
21CARYC34 /e/pyc_study (master|MERGING)
□□ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
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")
21CARYC34 /e/pyc_study (master|MERGING)
□□
解决冲突:手工编辑冲突文件并提交
我们可以直接查看README.md的内容:
1 # README #
2 本文件由Bitbucket创建Repository时创建,首次由PyCharm进行编辑。
3 通过PyCharm
4 - 提交(Ctrl+K)
5 - 推送(Ctrl+Shift+K) 与搜狗输入法快捷键冲突。
6
7 此处由Bitbucket进行编辑,需PyCharm更新才可以观察到更改。
8 - 更新(Ctrl+T)
9
10 ## 此处由carysLaptop小黑修改。
11
12 This README would normally document whatever steps are necessary to get your application up and running.
13
14 ## 0423 HaierLaptop modified
15 Creating a new branch is quick.
16
17 <<<<<<< HEAD
18 ## master
19 Creating a new branch is quick & simple.
20 =======
21 ## git branch 'feature1'
22 Creating a new branch is quick AND simple.
23
24
25
26 >>>>>>> feature1
27
28 ### What is this repository for? ###
Git用<<<<<<<
,=======
,>>>>>>>
标记出不同分支的内容,我们修改如下后保存:
Creating a new branch is quick and simple.
再提交:
21CARYC34 /e/pyc_study (master|MERGING)
□□ vim README.md
21CARYC34 /e/pyc_study (master|MERGING)
□□ git add README.md
21CARYC34 /e/pyc_study (master|MERGING)
□□ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
.README.md.swp
21CARYC34 /e/pyc_study (master|MERGING)
□□ git commit -m "conflict fixed"
[master bdffc59] conflict fixed
21CARYC34 /e/pyc_study (master)
□□ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
.README.md.swp
nothing added to commit but untracked files present (use "git add" to track)
21CARYC34 /e/pyc_study (master)
□□ rm .README.md.swp
21CARYC34 /e/pyc_study (master)
□□ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
21CARYC34 /e/pyc_study (master)
□□
查看分支
现在,master
分支和feature1
分支变成了下图所示:
用带参数的git log
也可以看到分支的合并情况:
21CARYC34 /e/pyc_study (master)
□□ git log --graph --pretty=oneline --abbrev-commit
* bdffc59 (HEAD -> master) conflict fixed
|\
| * 53a3d1f (feature1) AND simple
* | d71bde7 & simple
|/
* 915ed39 (origin/master, origin/HEAD) 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
21CARYC34 /e/pyc_study (master)
□□ git log --graph --pretty=oneline
* bdffc59a810cf74cc9e364b306c0ada677560bf4 (HEAD -> master) conflict fixed
|\
| * 53a3d1fab392c02318a31e708290381e77b7ff73 AND simple
* | d71bde74723082e9448543532b1d3a0f42a16e5b & simple
|/
* 915ed39bfbced0fda0a9d521a54874e544d563f3 (origin/master, origin/HEAD) branch dev modified
* 2d69ef50aec19fd3172ac24a3f943d39b9f7509b modify main.py from little black
* 40e53821936a0406a38f4aeba1fb80e0d1ca7c45 from carysLaptop little black
* 1aa2f87574ec03c9f4d2f20521d1c9a1344f0bea README.md 已在 Bitbucket 中,在线编辑过。
* a2252ecec2970f8c8ecd386814a25a3bfd3884ae modify readme.md
* 5cd5ad8f696e11287698a7dd57a12492c69d5319 from haierLaptop PyCharm Ver01
* 2d5f4c7f3c4bc44c5ff791f88788c5f51f0336d7 Initial commit
21CARYC34 /e/pyc_study (master)
□□ git branch
feature1
* master
21CARYC34 /e/pyc_study (master)
□□ git branch -d feature1
Deleted branch feature1 (was 53a3d1f).
21CARYC34 /e/pyc_study (master)
□□
小结
当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。
解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容,再提交。
用git log --graph
命令可以看到分支合并图。