详解git合并冲突解决方法

作者:ZhangRuoxu 时间:2023-05-11 05:18:34 

1、git merge冲突了,根据提示找到冲突的文件,解决冲突

如果文件有冲突,那么会有类似的标记

2、修改完之后,执行git add 冲突文件名

3、git commit

注意:没有-m选项

进去类似于vim的操作界面,把conflict相关的行删除掉

4、直接push就可以了,因为刚刚已经执行过相关merge操作了

相关的操作如下

冲突产生


[root@Monitor Demo]# git branch #当前在master分支下
* master
psr/psr-01
psr/psr-02
[root@Monitor Demo]# git checkout psr/psr-02 #切换到psr/psr-02分支下
Switched to branch 'psr/psr-02'
[root@Monitor Demo]# git branch
master
psr/psr-01
* psr/psr-02
[root@Monitor Demo]# ls
LICENSE new_remote_branch.txt psr_psr-02.txt README.md
[root@Monitor Demo]# vim psr_psr-02.txt #修改psr/psr-02分支上的文件
[root@Monitor Demo]# git add psr_psr-02.txt
[root@Monitor Demo]# git commit -m 'psr_psr-02.txt has changed on psr/psr-02 branch' #提交到暂存区
[psr/psr-02 62ca72c] psr_psr-02.txt has changed on psr/psr-02 branch
1 files changed, 6 insertions(+), 0 deletions(-)
[root@Monitor Demo]# git checkout master #切换到master分支下
Switched to branch 'master'
[root@Monitor Demo]# vim psr_psr-02.txt #在master分支下也对psr_psr-02.txt进行修改
[root@Monitor Demo]# git add psr_psr-02.txt
[root@Monitor Demo]# git commit -m 'changed this file on master branch'
[master 282fbeb] changed this file on master branch
1 files changed, 2 insertions(+), 0 deletions(-)
[root@Monitor Demo]# git merge psr/psr-02 #把psr/psr-02分支合并到当前分支,这时提示冲突了
Auto-merging psr_psr-02.txt
CONFLICT (content): Merge conflict in psr_psr-02.txt
Automatic merge failed; fix conflicts and then commit the result.

冲突解决过程

冲突文件的格式基本如下
<<<<<<<到=======是在当前分支合并之前的文件内容
=======到>>>>>>> psr/psr-02是在其它分支下修改的内容
需要在这个两个版本中选择一个,然后把标记符号也要一起删除
<<<<<<< HEAD


add some lines on master branch

add some lines on psr/psr-01 branch

2016年12月13日14:43:34 changed after psr/psr-02
=======
1
2
3
4
5
>>>>>>> psr/psr-02

冲突文件


vim psr_psr-02.txt

<<<<<<< HEAD
add some lines on master branch

add some lines on psr/psr-01 branch

2016年12月13日14:43:34 changed after psr/psr-02
=======
1
2
3
4
5
>>>>>>> psr/psr-02

修改冲突文件


# vim psr_psr-02.txt
README.md

I'am in new branch psr/psr-02 based on psr/psr-01

add some lines on master branch

add some lines on psr/psr-01 branch

2016年12月13日14:43:34 changed after psr/psr-02

添加冲突的文件,然后就可以直接push了


Merge branch 'psr/psr-02'

Conflicts:
 psr_psr-02.txt
#
# It looks like you may be committing a MERGE.
# If this is not correct, please remove the file
#  .git/MERGE_HEAD
# and try again.
#

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#

需要删掉下面这两行


Conflicts:
 psr_psr-02.txt

执行git push 操作


git push origin master

来源:https://www.cnblogs.com/ZhangRuoXu/p/6706571.html

标签:git,合并冲突
0
投稿

猜你喜欢

  • Linux 中PHP的安装教程

    2023-10-07 20:34:21
  • javascript将数字转换整数金额大写的方法

    2024-04-30 10:08:34
  • pygame游戏之旅 添加碰撞效果的方法

    2022-11-04 06:45:46
  • 只用50行Python代码爬取网络美女高清图片

    2023-08-29 07:45:51
  • 实例讲解Python的函数闭包使用中应注意的问题

    2022-05-17 23:29:53
  • LangChain简化ChatGPT工程复杂度使用详解

    2022-10-21 22:25:34
  • Python玩转PDF的各种骚操作

    2023-07-07 17:51:59
  • 详细分析Python垃圾回收机制

    2021-04-18 02:43:43
  • MySQL InnoDB 二级索引的排序示例详解

    2024-01-15 17:11:53
  • 详解Python中的文件操作

    2022-08-26 03:35:18
  • 如何在python中实现ECDSA你知道吗

    2023-03-14 14:07:17
  • 安装MySQL的步骤和方法

    2009-07-30 08:38:00
  • 用CSS设计多种文本框与按钮样式风格

    2007-08-10 13:11:00
  • vue实现在v-html的html字符串中绑定事件

    2023-07-02 16:44:08
  • Pandas 缺失数据处理的实现

    2023-07-14 05:57:38
  • 浅析Python中的赋值和深浅拷贝

    2021-09-10 14:30:14
  • SQLServer注释快捷键操作

    2024-01-25 20:45:38
  • Mysql实现增量恢复的方法详解

    2024-01-13 18:10:47
  • python 实现打印扫描效果详情

    2022-04-20 18:29:50
  • python3使用Pillow、tesseract-ocr与pytesseract模块的图片识别的方法

    2022-10-17 05:26:04
  • asp之家 网络编程 m.aspxhome.com