解决合并请求中的冲突
当我们项目越来越大,分支越来越多的时候,在合并分支时,就极有可能出现分支冲突,那我们需要怎么来解决冲突呢?在本小节的课程内容中,我们将通过 git 的实际操作来了解 git 合并请求冲突的产生以及解决办法。
在feature1
分支上提交:
$ git add readme.txt
$ git commit -m "AND simple"
[feature1 2443c95] AND simple
1 file changed, 1 insertion(+), 1 deletion(-)
然后推送到远程 origin/feature1
分支:
$ git push origin feature1
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 1.10 KiB | 1.10 MiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
To codechina.csdn.net:xiongjiamu/learning-git.git
* [new branch] feature1 -> feature1
接下来,让我们切换到master
分支:
$ git switch master
Switched to branch 'master'
在master分支上把readme.txt
文件的最后一行改为:
Creating a new branch is quick & simple.
提交:
$ git add readme.txt
$ git commit -m "& simple"
[master 06e5e3a] & simple
1 file changed, 1 insertion(+), 1 deletion(-)
然后推送到远程 origin/master
分支:
$ git push origin master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 1.10 KiB | 1.10 MiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
To codechina.csdn.net:xiongjiamu/learning-git.git
4aac6c7..06e5e3a master -> master
现在,master
分支和feature1
分支各自都分别有新的提交,变成了这样:
这种情况下,Git 无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,我们试试看:
$ git merge feature1
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
果然冲突了!Git 告诉我们,readme.txt
文件存在冲突,必须手动解决冲突后再提交。git status
也可以告诉我们冲突的文件:
git status
$ git status
On branch master
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.txt
no changes added to commit (use "git add" and/or "git commit -a")
接下来的操作,我们就可以直接在刚才新创建的 [合并请求] 中来进行操作了。
首先我们查看readme.txt
的内容:
合并冲突详情
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
Git 用<<<<<<<,=======,>>>>>>>
标记出不同分支的内容,我们直接在 [合并请求]中对文件内容进行修改如下,并提交:
Creating a new branch is quick and simple.
现在,master
分支和feature1
分支变成了下图所示:
如果你是在本地解决的冲突,在冲突解决后,你也可以在本地 git 中用带参数的 git log 也可以看到分支的合并情况:
git log
$ git log --graph --pretty=oneline --abbrev-commit
* fb8b190 (HEAD -> master) conflict fixed
|\
| * 2443c95 (feature1) AND simple
* | 06e5e3a & simple
|/
* 4aac6c7 branch test
* c47983c remove test.txt
* 9c18d80 add test.txt
* f3c7825 git tracks changes
* 7a0050a understand how stage works
* 187f963 append GPL
* e55063a add distributed
* 50ed06b wrote a readme file
最后,删除feature1
分支:
$ git branch -d feature1
Deleted branch feature1 (was 2443c95).
好了,到这里冲突解决就完成了,我们修改的内容也可以顺利的合并到master
分支上了。
好了,让我们来回顾一下:
- 当 git 无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。
- 解决冲突就是把 git 合并失败的文件手动编辑为我们希望的内容,再提交。
评论区