为什么会有冲突
如果两个人同时编辑一个文件的同一行,然后一方先提交,另一方也编辑了,然后更新了,导致的结果就是有可能发生冲突;所谓冲突就是Git不支持该选那一部分代码了,要你手动来选择,选择好了告诉Git。
下面我们以两个同事来模拟这个过程。
新同事编辑了文件
首先我们先模拟新同事又编辑了index.html文件内容如下:
Hello index new hi
然后依次执行如下命令:
#添加文件到暂存区
git add index.html
#提交变更
git commit -m "update index file hi"
#将变更推送到远程仓库(这里的远程仓库是Github)
git push
现在你可以去Github确认下编辑的文件是否提交上去了。
另外一个同事也编辑该文件
现在另外一个同事也来修改index.html文件内容如下:
Hello index new other
现在他也一次执行如下命令:
git add index.html
git commit -m "add other in index.html file"
git push
但是这次执行push命令好像和以前的提示不太一样,输出如下:
#前面有可能有一部分输出,我们这里省略了,因为他不是我们这个问题的关键To https://github.com/ixueaedu/first1.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/ixueaedu/first1.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
大概意思就是push失败,你需要先执行git pull命令。因为远程的该文件已经被新同事修改了,如果我这次push上去了,那这个里面的最后一个单词到底是hi还是other呢?这个时候Git就不知道了,需要我们去解决,解决的方法是看你了,选择是hi,还是other,还是两者都要,还是都不要给。我们这里选择两者都不要。
于是我们按照提示执行了git pull,输出如下:
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/ixueaedu/first1
a3734e9..5146296 master -> origin/master
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
发现这次和上次git pull输出又不太一样,关键点就是说自动合并index.html出错了,有冲突,你需要手动解决。
用git status命令查看一下状态(该命令经常使用)
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add ..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
可以发现index.html文件前面的状态是both modified,这表示该文件有冲突,同时上面还有提示说使用git add命令将文件标记为已经解决了的。
现在我们这里打开index.html文件查看内容如下:
<<<<<<< HEAD
Hello index new other
=======Hello index new hi
>>>>>>> 514629604932a47d2894360215ef8e9fc133f6c1
可以发现该文件中又<<<<<<<,=======,>>>>>>>这样的符号,这其实是Git用来分割同一个文件不同版本的方法。这里要注意的是不同的Git工具表示这个意思的方法可能不同,如果你使用的其他客户的可能产生的是多个文件,而我们这里使用的Git命令行,他是这种表示方法。
其中<<<<<<< HEAD到=======表示我们自己更改的(也就是执行git pull命令的这人),=======到>>>>>>> 514629604932a47d2894360215ef8e9fc133f6c1表示是远程仓库上的修改。
现在要做的就是讲这里的内容更改正确,并移除这些符号,于是我们更改的内容如下:
Hello index new 我们这里删除了这些符号,同时将内容更改为你认为正确的!
最关键的一步就是,使用git add命令将这个文件标记为已经解决的。
git add index.html
然后再次查看状态:
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: index.html
可以看到现在文件的状态已经变成了modified,说明冲突已经解决了,先要做的就是将这个更改提交到本地。
git commit -m "fix merge conflict"
先可以把代码push到远程仓库,其他的开发人员前面这章节讲解的这些内容知道完成一个项目。
当然你也可能过一会儿在push,比如你现在没有网络,但推荐是立即push。
本文来自《完全掌握Git电子书》,视频教程也可以通过:爱学啊官网 、腾讯课堂、网易云课堂、淘宝教育学习。

网友评论