【Git】合并冲突

ops/2025/3/10 18:56:36/

合并冲突

可是,在实际分支合并的时候,并不是想合并就能合并成功的,有时候可能会遇到代码冲突的问题。

为了演示这问题,创建一个新的分支 dev1 ,并切换至目标分支,我们可以使用 git checkout -b dev1 一步完成创建并切换的动作,示例如下:

$ git checkout -b dev1
Switched to a new branch 'dev1'$ git branch
* dev1master

dev1 分支下修改 ReadMe 文件,更改文件内容如下,并进行一次提交,如:

$ cat ReadMe
hello bit
hello git
hello world
hello version1
hello version2
hello version3
write bbb for new branch # 将aaa该为bbb$ git add.
$ git commit -m"modify ReadMe"
[dev1 0854245] modify ReadMe1 file changed, 1 insertion(+), 1 deletion(-)

切换至 master 分支,观察 ReadMe 文件内容:

$ git checkout master
Switched to branch'master'$ cat ReadMe
hello bit
hello git
hello world
hello version1
hello version2
hello version3
write aaa for new branch

我们发现,切回来之后,文件内容变成了老的版本,这种现象很正常,我们现在也完全能理解。
此时在 master 分支上,我们对 ReadMe 文件再进行一次修改,并进行提交,如下:

$ git branch
dev1
* master$ vim ReadMe
$ cat ReadMe
hello bit
hello git
hello world
hello version1
hello version2
hello version3
write ccc for new branch$ git add.
$ git commit -m"modify ReadMe"
[master 1c10f6d] modify ReadMe1 file changed, 1 insertion(+), 1 deletion(-)

现在,master 分支和 dev1 分支各自都分别有新的提交,变成了这样:
在这里插入图片描述

这种情况下,Git 只能试图把各自的修改合并起来,但这种合并就可能会有冲突,如下所示:

$ git merge dev1
Auto - merging ReadMe
CONFLICT (content): Merge conflict in ReadMe
Automatic merge failed; fix conflicts and then commit the result.$ 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
no changes added to commit (use "git add" and/or "git commit -a")

发现 ReadMe 文件有冲突后,可以直接查看文件内容,要说的是 Git 会用 <<<<<<<=======>>>>>>> 来标记出不同分支的冲突内容,如下所示:

$ cat ReadMe
hello bit
hello git
hello world
hello version1
hello version2
hello version3
<<<<<<< HEAD
write ccc for new branch
=======
write bbb for new branch
>>>>>>> dev1

此时我们必须要手动调整冲突代码,并需要再次提交修正后的结果!!(再次提交很重要,切勿忘记)

$ cat ReadMe
hello bit
hello git
hello world
hello version1
hello version2
hello version3
write bbb for new branch$ git add.
$ git commit -m"merge ReadMe"
[master 2976afc] merge ReadMe

到这里冲突就解决完成,此时的状态变成了

在这里插入图片描述

用带参数的 git log 也可以看到分支的合并情况,具体大家可以自行搜索 git log 的用法:

$ git log --graph --pretty=oneline --abbrev-commit
* 2976afc (HEAD -> master) merge ReadMe
|\
| * c59f4d1 (dev1) modify ReadMe
* | c10f6d0 modify ReadMe
|/

最后,不要忘记 dev1 分支使用完毕后就可以删除了:

$ git branch
* master$ git branch -d dev1
Deleted branch dev1 (was c59f4d1).

分支管理策略

通常合并分支时,如果可能,Git 会采用 Fast forward 模式。还记得如果我们采用 Fast forward 模式之后,形成的合并结果是什么样呢?回顾一下

在这里插入图片描述

在这种 Fast forward 模式下,删除分支后,查看分支历史时,会丢掉分支信息,看不出来最新提交到底是从哪个分支过来的。

但在合并时如果发生了冲突,我们还是正常提交解决冲突问题,会再进行一次新的提交,得到的最终状态为:

在这里插入图片描述

那么就不是 Fast forward 模式了,这样的好处是,从分支历史上就可以看出分支信息。
例如我们现在已经删除了在合并冲突部分创建的 dev1 分支,但依旧能看到 master 其实是由其他分支合并得来的。

$ git log --graph --pretty=oneline --abbrev-commit
* 2976afc (HEAD -> master) merge ReadMe
|\
| * c59f4d1 modify ReadMe
* | c10f6d0 modify ReadMe
|/

Git 支持我们强制禁用 Fast forward 模式,那么就会在 merge 时生成一个新的 commit ,这样,从分支历史上就可以看出分支信息。
下面我们实战一下 --no-ff 方式的 git merge 。首先,创建新的分支 dev2 ,并切换至新的分支:

$ git checkout -b dev2
Switched to a new branch 'dev2'

修改 ReadMe 文件,并提交一个新的 commit

$ cat ReadMe
hello bit
hello world
hello git
hello version1
hello version2
hello version3
write bbb for new branch$ git add.
$ git commit -m"modify ReadMe"
[dev2 41b82f7] modify ReadMe1 file changed, 1 insertion(+)

切回 master 分支,开始合并:

$ git checkout master
Switched to branch'master'$ git merge --no-ff -m "merge with --no-ff" dev2
Merge made by the 'recursive' strategy.
ReadMe | 1 +
1 file changed, 1 insertion(+)$ cat ReadMe
hello bit
hello world
hello git
hello version1
hello version2
hello version3
write bbb for new branch
a,b,c,d

请注意 --no-ff 参数,表示禁用 Fast forward 模式。禁用 Fast forward 模式后合并会创建一个新的 commit ,所以加上 -m 参数,把 merge 信息写进去。

合并后,查看分支历史:

$ git log --graph --pretty=oneline --abbrev-commit
*   5bde1b4 (HEAD -> master) merge with --no-ff
|\
| * 41b82f7 (dev2) modify ReadMe
* | c10f6d0 modify ReadMe
|/

可以看到,不使用 Fast forward 模式,merge 后像这样:
在这里插入图片描述

所以在合并分支时,加上 --no-ff 参数就可以保留分支信息,合并后的历史有分支,能看出来曾经做过合并,而 fast forward 合并就看不出来曾经做过合并。


http://www.ppmy.cn/ops/164730.html

相关文章

高考數學。。。

2024上 具体来说&#xff0c;直线的参数方程可以写为&#xff1a; x1t y−t z1t 二、简答题(本大题共5小题&#xff0c;每小题7分&#xff0c;共35分。) 12.数学学习评价不仅要关注结果评价&#xff0c;也要关注过程评价。简要说明过程评价应关注哪几个方面。…

【Linux-网络】HTTP的清风与HTTPS的密语

&#x1f3ac; 个人主页&#xff1a;谁在夜里看海. &#x1f4d6; 个人专栏&#xff1a;《C系列》《Linux系列》《算法系列》 ⛰️ 道阻且长&#xff0c;行则将至 目录 &#x1f4da; 引言 &#x1f4da; 一、HTTP &#x1f4d6; 1.概述 &#x1f4d6; 2.URL &#x1f5…

聊天室Python脚本——ChatGPT,好用

下面提供两个 Python 脚本&#xff0c;一个作为服务器端&#xff08;chat_server.py&#xff09;&#xff0c;一个作为客户端&#xff08;chat_client.py&#xff09;。你可以在一台电脑上运行服务器脚本&#xff0c;然后在不同电脑上运行客户端脚本&#xff08;连接时指定服务…

使用 Spring Boot 和 Spring Security 构建安全的 Web 应用:OAuth2、”记住我”与 JWT 集成指南

使用 Spring Boot 和 Spring Security 构建安全的 Web 应用&#xff1a;OAuth2、记住我与 JWT 集成指南 在现代 Web 应用中&#xff0c;安全性是至关重要的。Spring Security 是一个功能强大且高度可定制的安全框架&#xff0c;能够帮助我们轻松实现认证、授权以及其他安全功能…

【面试】Zookeeper

Zookeeper 1、ZooKeeper 介绍2、znode 节点里面的存储3、znode 节点上监听机制4、ZooKeeper 集群部署5、ZooKeeper 选举机制6、何为集群脑裂7、如何保证数据一致性8、讲一下 zk 分布式锁实现原理吧9、Eureka 与 Zk 有什么区别 1、ZooKeeper 介绍 ZooKeeper 的核心特性 高可用…

【贪心算法2】

力扣122.买卖股票最佳时机Ⅱ 链接: link 思路 要求最大利润&#xff0c;可以分解成子问题求解&#xff0c;在最低价格买入&#xff0c;最高价格卖出。 假如第0天价格最低&#xff0c;第3天价格最高&#xff0c;利润prices[3] - pricnes[0], 可以将利润公式拆解成 (prices[3]…

AI 智能:开拓未知疆域的科技先锋

在当今科技迅猛发展的浪潮中&#xff0c;AI 智能无疑是最耀眼的弄潮儿&#xff0c;持续重塑着我们生活与工作的方方面面。然而&#xff0c;在这片广袤的技术海洋里&#xff0c;还有诸多潜藏在深处、尚未被广泛挖掘与讨论的领域&#xff0c;它们代表着 AI 智能未来发展的新方向&…

【音视频】RTP封包H265信息

RTP 含义 RTP 是一种专门为 实时数据传输 设计的网络协议。这里的 "实时数据" 主要指的是 音频 和 视频 这类对传输延迟非常敏感的数据。想象一下&#xff0c;你在进行视频通话或者观看在线直播&#xff0c;你希望画面和声音能够流畅地、几乎同步地到达&#xff0c;而…