Git冲突处理

举报
yd_221104950 发表于 2020/12/02 23:27:04 2020/12/02
【摘要】 Git冲突很常见。本质是两个分支之间都提交了相同的东西,如: 同时修改了同一份文件创建了相同的文件 解决冲突的办法: 手动合并两个文件,各自再提交,然后再进行合并选择保留其中一份文件,删除掉另一份 同时修改了同一份文件引起的冲突及其解决方法例子: # 切换到master分支 ~/Desktop/MyApp$ git checkout status On bra...

Git冲突很常见。本质是两个分支之间都提交了相同的东西,如:

  • 同时修改了同一份文件
  • 创建了相同的文件

解决冲突的办法:

  • 手动合并两个文件,各自再提交,然后再进行合并
  • 选择保留其中一份文件,删除掉另一份

同时修改了同一份文件引起的冲突及其解决方法例子

# 切换到master分支
~/Desktop/MyApp$ git checkout status
On branch master
nothing to commit, working tree clean
~/Desktop/MyApp$ ls
app  H.txt
# 创建两个分支
~/Desktop/MyApp$ git branch tt01
~/Desktop/MyApp$ git branch tt02
# 修改tt01分支的H.txt文件
~/Desktop/MyApp$ git checkout tt01
Switched to branch 'tt01'
~/Desktop/MyApp$ ls
app  H.txt
~/Desktop/MyApp$ echo hello > H.txt
~/Desktop/MyApp$ git add H.txt
~/Desktop/MyApp$ git commit H.txt -m "init H.txt"
~/Desktop/MyApp$ cat H.txt
hello
# 修改tt02分支的H.txt文件
~/Desktop/MyApp$ git checkout tt02
~/Desktop/MyApp$ ls
app  H.txt
~/Desktop/MyApp$ echo hi > H.txt
~/Desktop/MyApp$ git add H.txt
~/Desktop/MyApp$ git commit H.txt -m "init H.txt"
~/Desktop/MyApp$ cat H.txt
hi
# 将分支tt02合并到分支tt01
~/Desktop/MyApp$ git checkout tt01
Switched to branch 'tt01'
# 合并tt02到tt01过程中,出现冲突
~/Desktop/MyApp$ git merge tt02
Auto-merging H.txt
CONFLICT (content): Merge conflict in H.txt
Automatic merge failed; fix conflicts and then commit the result.
~/Desktop/MyApp$ cat H.txt
<<<<<<< HEAD
hello
=======
hi
>>>>>>> tt02

~/Desktop/MyApp$ git status
On branch tt01
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:   H.txt

no changes added to commit (use "git add" and/or "git commit -a")
# 解决冲突,手动合并,将两个分支手动合并,然后各自commit,最后再合并分支
# 修改tt01分支的H.txt文件
~/Desktop/MyApp$ git checkout tt01
Switched to branch 'tt01'
~/Desktop/MyApp$ echo hello,hi > H.txt
~/Desktop/MyApp$ git add H.txt
~/Desktop/MyApp$ git commit H.txt -m "init H.txt"
~/Desktop/MyApp$ cat H.txt
hello,hi
# 修改tt02分支的H.txt文件
~/Desktop/MyApp$ git checkout tt02
~/Desktop/MyApp$ echo hello,hi > H.txt
~/Desktop/MyApp$ git add H.txt
~/Desktop/MyApp$ git commit H.txt -m "init H.txt"
~/Desktop/MyApp$ cat H.txt
hello,hi
# 再次合并
~/Desktop/MyApp$ git checkout tt01
Switched to branch 'tt01'
# 合并成功
~/Desktop/MyApp$ git merge tt02
Merge made by the 'recursive' strategy.

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

创建了相同的文件引起的冲突及其解决方法例子

# 切换到master分支
~/Desktop/MyApp$ git checkout master
Switched to branch 'master'
# 创建分支test01 、test02
~/Desktop/MyApp$ git branch test01
~/Desktop/MyApp$ git branch test02
# 在test01分支创建一个test.txt文件
~/Desktop/MyApp$ git checkout test01
Switched to branch 'test01'
~/Desktop/MyApp$ touch test.txt
~/Desktop/MyApp$ echo Hello test01>test.txt
~/Desktop/MyApp$ git add test.txt
~/Desktop/MyApp$ git commit test.txt -m "init test.txt of test01"
[test01 b323e2c] init test.txt of test01
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
# 在test02分支创建一个test.txt文件
~/Desktop/MyApp$ git checkout test02
Switched to branch 'test02'
~/Desktop/MyApp$ touch test.txt
~/Desktop/MyApp$ echo hello test02 > test.txt
~/Desktop/MyApp$ git add test.txt
~/Desktop/MyApp$ git commit test.txt -m "init test"
[test02 fd1e225] init test
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
# 将分支test01合并到test02
~/Desktop/MyApp$ git merge test01
Auto-merging test.txt
CONFLICT (add/add): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
# 通过git status可以查看冲突信息
~/Desktop/MyApp$ git status
On branch test02
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 added: test.txt

no changes added to commit (use "git add" and/or "git commit -a")
# 要先终止合并,才能去做别的事
~/Desktop/MyApp$ git merge --abort
~/Desktop/MyApp$ git status
On branch test02
nothing to commit, working tree clean
~/Desktop/MyApp$ ls
app  H.txt  test.txt
# 删除test02分支的test.txt文件,使用test01分支的test.txt文件
~/Desktop/MyApp$ git rm test.txt
rm 'test.txt'
~/Desktop/MyApp$ git commit -m "delete test.txt
> "
[test02 49c01a2] delete test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt
# 再次合并
~/Desktop/MyApp$ git merge test01
Merge made by the 'recursive' strategy.
 test.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

谢谢阅读!

文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_40763897/article/details/102931070

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。