第八节《冲突解决》

 在真实的运行环境中,用户的协同并不会一帆风顺,只要有合并就可能会有冲突,前面我们看到只要把共享版本库中的最新提交直接拉回到本地,然后就可以推送了,然而事实并不是这样....

<1>用户user1向版本库推送时,因为user2强制推送已经改变了共享版本库的提交状态,导致user1推送失败

<2>用户user1执行拉回操作的第一阶段,将共享版本库master分支的最新提交获取到本地,并更新到本地版本库特定的引用refs/remotes/origin/master

<3>用户user1执行拉回操作的第二阶段,将本地分支master和共享版本库本地跟踪分支origin/master进行合并

<4>用户user1执行推送操作,将本地提交推送到共享版本库

实际上git pull是由两个步骤组成:一个是git fetch和git merge。

 产生冲突的情况及解决办法:

1、两个用户修改了不同的文件

<1>用户user1修改team/user1.txt文件,提交并推送到共享服务器

[[email protected] project]# echo "hack by user1 at `date`" >> team/user1.txt
[[email protected] project]# git add -u
[[email protected] project]# git commit -m "update team/user1.txt"
[master 66cd761] update team/user1.txt
1 files changed, 1 insertions(+), 0 deletions(-)
[[email protected] project]# git push
Counting objects: 7, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 407 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
To file:///path/to/repos/shared.git
fbae0b3..66cd761 master -> master

<2>用户user2修改team/user2.txt文件提交

[[email protected] project]# echo "hack by user2 at `date`" >> team/user2.txt
[[email protected] project]# git add -u
[[email protected] project]# git commit -m "update team/user2.txt"
[master aada835] update team/user2.txt
1 files changed, 1 insertions(+), 0 deletions(-)

<3>用户user2在推送时会遇到非快进式推送错误

[[email protected] project]# git push
To file:///path/to/repos/shared.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ‘file:///path/to/repos/shared.git‘
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the ‘Note about
fast-forwards‘ section of ‘git push --help‘ for details.

<4>用户user2执行git fetch操作,获取到的提交更新到本地用于跟踪共享版本库master分支的本地引用origin/master中

[[email protected] project]# git fetch
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From file:///path/to/repos/shared
fbae0b3..66cd761 master -> origin/master

<5>用户user2执行合并操作,完成自动合并

[[email protected] project]# git merge origin/master
Merge made by recursive.
team/user1.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)

<6>用户user2推送合并后的本地版本库

[[email protected] project]# git push
Counting objects: 12, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 749 bytes, done.
Total 7 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
To file:///path/to/repos/shared.git
66cd761..5a0af89 master -> master

<7>查看提交日志

[[email protected] project]# git log -3 --graph --stat
* commit 5a0af89a0b71f10358c5d7034d9ec9ff5bad4332
|\ Merge: aada835 66cd761
| | Author: user2 <[email protected]>
| | Date: Tue Aug 29 16:03:48 2017 +0800
| |
| | Merge remote branch ‘origin/master‘
| |
| * commit 66cd7610ba57a5da5b75ee0234ebf5d4330c2cac
| | Author: user1 <[email protected]>
| | Date: Tue Aug 29 15:59:27 2017 +0800
| |
| | update team/user1.txt

team/user1.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
| |
* | commit aada835b917d93d9b31820ca413325fa23f84671
|/ Author: user2 <[email protected]>
| Date: Tue Aug 29 16:01:00 2017 +0800
|
| update team/user2.txt

team/user2.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)

2、修改相同文件的不同区域

<1>用户user1修改README文件,在第一行插入User1 hacked

[[email protected] project]# cat README
User1 hacked.
Hello

<2>用户user1对修改进行本地提交并推送到共享版本库

[[email protected] project]# git add -u
[[email protected] project]# git commit -m "User1 hack at the beginning."
[master f0595a9] User1 hack at the beginning.
1 files changed, 1 insertions(+), 0 deletions(-)
[[email protected] project]# git push
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 292 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To file:///path/to/repos/shared.git
5a0af89..f0595a9 master -> master

<3>用户user2修改自己工作区中的README文件,在最后插入User2 hacked.

[[email protected] project]# cat README
Hello
User2 hacked.

<4>用户user2对修改进行本地提交

[[email protected] project]# git add -u
[[email protected] project]# git commit -m "User2 hack at the end."
[master b255a59] User2 hack at the end.
1 files changed, 1 insertions(+), 0 deletions(-)

<5>用户user2执行git fetch操作,获取到的提交更新到本地

[[email protected] project]# git fetch
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From file:///path/to/repos/shared
5a0af89..f0595a9 master -> origin/master

<6>用户user2执行合并操作,并推送

[[email protected] project]# git merge refs/remotes/origin/master
Auto-merging README
Merge made by recursive.
README | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
[[email protected] project]# git push
Counting objects: 10, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 592 bytes, done.
Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
To file:///path/to/repos/shared.git
f0595a9..95fa871 master -> master

3、同时更改文件名和文件内容

<1>用户user1在自己的工作区将文件README进行重命名,本地提交并推送

[[email protected] project]# pwd
/path/to/user1/workspace/project
[[email protected] project]# mkdir doc
[[email protected] project]# git mv README doc/README.txt
[[email protected] project]# git commit -m "move document to doc/."
[master 62a8eee] move document to doc/.
1 files changed, 0 insertions(+), 0 deletions(-)
rename README => doc/README.txt (100%)
[[email protected] project]# git push
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 299 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To file:///path/to/repos/shared.git
95fa871..62a8eee master -> master

<2>用户user2在自己的工作区中修改README文件,在文件最后抽入内容,执行本地提交

[[email protected] project]# pwd
/path/to/user2/workspace/project
[[email protected] project]# echo "User2 hacked again." >> README
[[email protected] project]# git add -u
[roo[email protected] project]# git commit -m "User2 hack README again."
[master 7a6872f] User2 hack README again.
1 files changed, 1 insertions(+), 0 deletions(-)

<3>用户user2执行git fetch操作,获取到的提交更新到本地

[[email protected] project]# git fetch
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From file:///path/to/repos/shared
95fa871..62a8eee master -> origin/master

<4>用户user2执行合并操作,并推送

[[email protected] project]# git merge refs/remotes/origin/master
Merge made by recursive.
README => doc/README.txt | 0
1 files changed, 0 insertions(+), 0 deletions(-)
rename README => doc/README.txt (100%)

[[email protected] project]# git push
Counting objects: 10, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 623 bytes, done.
Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
To file:///path/to/repos/shared.git
62a8eee..6809fc0 master -> master

4、更改文件的同一内容

<1>用户user1修改doc/README.txt文件第二行

[[email protected] project]# cat doc/README.txt
User1 hacked.
Hello,user1.
User2 hacked.

<2>user1对修改进行提交和推送

[[email protected] project]# git add -u
[[email protected] project]# git commit -m "say hello to user1."
[master 7234438] say hello to user1.
1 files changed, 1 insertions(+), 1 deletions(-)

[[email protected] project]# git push
Counting objects: 14, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (8/8), 706 bytes, done.
Total 8 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
To file:///path/to/repos/shared.git
6809fc0..e9e7941 master -> master

<3>用户user2也更改README.txt文件

[[email protected] project]# cat doc/README.txt
User1 hacked.
Hello,user2.
User2 hacked.
User2 hacked again.

<4>user2进行提交

[[email protected] project]# git add -u
[[email protected] project]# git commit -m "say hello to user2."
[master 16bb0a2] say hello to user2.
1 files changed, 1 insertions(+), 1 deletions(-)

<5>user2执行pull操作,遇到冲突

[[email protected] project]# git pull
remote: Counting objects: 14, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 8 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
From file:///path/to/repos/shared
6809fc0..e9e7941 master -> origin/master
Auto-merging doc/README.txt
CONFLICT (content): Merge conflict in doc/README.txt
Automatic merge failed; fix conflicts and then commit the result.

<6>查看一下现在的文件

[[email protected] project]# cat doc/README.txt
User1 hacked.
<<<<<<< HEAD
Hello,user2.                                                       //<<<和===之间的内容是当前分支所更改的内容
=======
Hello,user1.                                                      //===和>>>之间的内容是所合并的版本更改的内容
>>>>>>> e9e7941f0e86186bd24b6e251ce3ee9deaf82931
User2 hacked.
User2 hacked again.

<7>手工编辑完成冲突解决

[[email protected] project]# vim doc/README.txt

User1 hacked.
Hello,user1 and user2.
User2 hacked.
User2 hacked again.

<8>执行提交和推送

[[email protected] project]# git add -u
[[email protected] project]# git commit -m "Merge completed: say hello to all users."
[master 5ca3509] Merge completed: say hello to all users.
[[email protected] project]# git push
Counting objects: 14, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 710 bytes, done.
Total 8 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
To file:///path/to/repos/shared.git
e9e7941..5ca3509 master -> master

时间: 2024-10-09 02:25:50

第八节《冲突解决》的相关文章

Git的冲突解决过程

下面图是我总结一次提交遇到冲突解决的过程. 1. 把本地工作区的修改提交到本地仓库 2. 从远程仓库拉取代码,与本地仓库合并(pull = fetch + merge) 3. 本地仓库的代码推送回工作区,包括confilct部分 4. 解决冲突,然后重新commit到本地仓库 5. push到远程仓库,完成一次有冲突的提交

双击和单击事件冲突解决方法

双击和单击事件冲突解决方法:关于单击事件和双击事件,如果单独拿出来都是非常简单,这里也就不多介绍了,具体可以参阅jQuery的click事件和jQuery的dblclick事件一章节,但是如果同一个元素同时绑定了单击事件和双击事件,那么就会造成冲突现象,比如我们双击此元素指向触发双击事件,但是也完全满足单击事件的条件,不但双击事件触发了,单击事件同样也会被触发,下面就通过代码实例介绍一下如何解决它们的冲突问题,代码如下: <!DOCTYPE html> <html> <hea

(转)ViewPager,ScrollView 嵌套ViewPager滑动冲突解决

ViewPager,ScrollView 嵌套ViewPager滑动冲突解决 本篇主要讲解一下几个问题 粗略地介绍一下View的事件分发机制 解决事件滑动冲突的思路及方法 ScrollView 里面嵌套ViewPager导致的滑动冲突 ViewPager里面嵌套ViewPager 导致的滑动冲突 轮播图的几种实现方式 先看一下效果图 ScrollView里面嵌套ViewPager ViewPager里面嵌套ViewPager View的 事件分发机制 这篇博客大打算详细讲解View的事件分发机制

svn conflict 冲突解决

转自:http://www.gezila.com/tutorials/17290.html 目录: 1. 同一处修改文件冲突 1.1. 解决方式一 1.2. 解决方式二 1.3. 解决总结 2. 手动解决冲突 2.1. 冲突背景1 2.2. 冲突背景2 2.3. 冲突解决 1. 同一处修改文件冲突 开发人员都知道代码管理工具是开发中一个必不可少的工具,这里也不废话详细介绍了.不管你个人喜欢git还是svn还是其他,但还有一大部分公司在使用svn做代码管理工具.这里详细介绍下SVN提交文件时冲突问

apache与IIS共用80端口冲突解决方法

如果同一台电脑安装了apache和iis,会提示80端口冲突,如何解决apache与iis 80端口冲突的问题呢,并且同时使用apache和iis 将apache设为使用80端口,IIS使用其它端口,比如81,然后将apache作为IIS的代理. 在httpd.conf里面,取消下面四行的注释: LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_connect_module modules/mod_proxy_connec

[BetterExplained]亲密关系中的冲突解决

一般来说,解决冲突的办法是两人都不越界要求对方,尊重对方的意愿,但一旦双方皆认为自己合理,冲突却不可避免的时候,就必须各自退回到尊重对方的自由. 但其实这也不是问题的终极解决之道,因为这会导致一旦冲突发生,两人就退回到自己的领地,制造出冷战气氛来,“尊重对方”说起来容易,做起来着实没有那么容易. 这里有一个更好的方案,简言之就是两人都将双方看成一个利益共同体. 现实生活中人们其实很难将他人的利益真正放在心上,不过亲密关系中的人如果想最优化相处的话还必须得这么做,因为本身两人的利益就是一个整体.

sublime text2打开控制台快捷键ctrl+`冲突解决

sublime text2 打开控制台(Show Console)的方法: View -> Show Console 快捷键是: ctrl+`(数字1左边的按键) 但是有些windows用户可能会出现快捷键无效的问题, 一般就是和系统快捷键冲突了, 我的问题就是和QQ拼音的快捷键冲突了,我们需要把系统的ctrl+`的快捷键去掉 和QQ拼音快捷键冲突解决方法如下: 打开QQ拼音的属性设置, 找到输入法管理 将热键切换至QQ拼音 前的勾选去掉,或者改一个别的快捷键 吐槽: QQ拼音默认是ctrl+~

Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法

如下面所示,同时导入这两个,会提示其中一个与另一个产生冲突. 1 import android.view.View.OnClickListener; 2 import android.content.DialogInterface.OnClickListener; 其实,当我们用某个Listener时,不一定就要import它,直接用全名去定义就不需要import了,例如 1 mButton1.setOnClickListener(new OnClickListener() 2 { 3 4 @O

IIS6 2.0 4.0 冲突解决 &#39;c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\xxx&#39; -- &#39;Access is denied. &#39;

今天在阿里云虚拟机上部署新站点后出现下面的错误: Compiler Error Message: CS0016: Could not write to output file 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\xx' -- 'Access is denied. ' 找遍了网上的资料最后总结为以下几步:: Cleaned the "C:\Windows\Microsoft.NE