网站首页 > 技术文章 正文
合并(merge)是很常用的操作。尤其是一个庞大的很多人参与开发的企业级应用。一般会设定一个主分支,和多个副分支。
在副分支开发完成后,合并到主分支中。始终保持主分支是一个完整的,稳定的最新状态的分支。
那么合并必然带来一些问题,比如最常见的冲突问题。
合并冲突
首先初始化一个仓库用于测试合并的冲突问题。建立一个文件,内容如下:
文件内容:
this is the 1 line. this is the 2 line. this is the 3 line. this is the 4 line. this is the 5 line.
将文件加入缓存,然后提交。
这时在当前分支基础上,切换到新的分支whitespace,修改第三行。文件内容如下
this is the 1 line. this is the 2 line. this is the 3 line. Changed. this is the 4 line. this is the 5 line.
将这个文件也提交。然后确认一下两个分支的状态。
$ git log
commit f707a64fe9e2b39bd60be414eb11c61f8e811bc1 (HEAD -> master)
Author: kutilion <kutilion@gmail.com>
Date: Fri Apr 12 17:51:51 2019 +0800
1st commit
$ git log
commit 699ea51361645afa83d871be47518cc14b52ea4c (HEAD -> whitespace)
Author: kutilion <kutilion@gmail.com>
Date: Fri Apr 12 17:56:26 2019 +0800
1st commit on whitespace branch
commit 96b5bc8cb0a8e71099a4e2908e8dc1256e0c0dd6
Author: kutilion <kutilion@gmail.com>
Date: Fri Apr 12 17:51:51 2019 +0800
1st commit
现在返回master分支,修改master分支下的文件,内容如下。
this is the 1 line. this is the 2 line. this is the 3 line. Conflicted. this is the 4 line. this is the 5 line.
这时, 两个分支的第三行已经产生了冲突。执行合并操作会出现错误:
$ git merge whitespace
Auto-merging MergeTest.txt
CONFLICT (content): Merge conflict in MergeTest.txt
Automatic merge failed; fix conflicts and then commit the result.
冲突的文件内容:
this is the 1 line.
this is the 2 line.
<<<<<<< HEAD
this is the 3 line. Conflicted.
=======
this is the 3 line. Changed.
>>>>>>> whitespace
this is the 4 line.
this is the 5 line.
确认一下分支状态,也可以看到冲突提示。
$ git status -sb
## master
UU MergeTest.txt
取消这次失败的merge可以使用如下命令
$ git merge --abort
再次确认分支状态,发现冲突提示没有了。回复到了合并前的状态。但是要注意,如果当前分支由未被追踪的文件,可能会被删除。
$ git status -sb
## master
当然,也可以使用reset –hard HEAD命令来强行返回合并前的状态。
手动合并文件
出现冲突后,可以手动解决。这时需要确认三个版本的文件,执行合并命令的分支的文件,被合并分支的文件,以及它们共通的先祖文件
可以使用show命令把这三个文件拷贝出来
$ git show :1:MergeTest.txt > MergeTest.ancestor.txt
$ git show :2:MergeTest.txt > MergeTest.master.txt
$ git show :3:MergeTest.txt > MergeTest.whitespace.txt
官网推荐使用merge-file命令和上述三个文件来合并。但是笔者不推荐,笔者推荐直接手动修改冲突文件,然后再次提交。
$ git add MergeTest.txt
$ git commit -m "Merge conflict solved"
[master d102b7c] Merge conflict solved
确认解决后的合并状态
$ git log --oneline
47e0c00 (HEAD -> master) Merge conflict solved
1bc9fcc 2st commit on master branch
77fbf54 (whitespace) 1st commit on whitespace branch
efbb750 1st commit on master branch
- 上一篇: git如何撤销合并(git退出合并)
- 下一篇: git命令梳理(git的命令)
猜你喜欢
- 2024-10-09 git各种操作:基本操作 and 多人协作 and 冲突解决
- 2024-10-09 通过图解的方式学习常用Git命令(git图文教程)
- 2024-10-09 Git实战005:如何正确的使用同步、获取和拉取指令
- 2024-10-09 相见恨晚的 Git 命令动画演示,一看就懂
- 2024-10-09 Git 工作流程:从基础到高效协作(git的操作流程)
- 2024-10-09 学无止境:Git 如何优雅地回退代码
- 2024-10-09 git常用命令(git常用命令恢复文件)
- 2024-10-09 Git常用命令(git常用命令总结)
- 2024-10-09 git命令梳理(git的命令)
- 2024-10-09 git 常用命令(git常用命令大全菜鸟)
- 04-29kali2021ping 外网不通
- 04-29我是如何用这3个小工具,助力小姐姐提升100%开发效率的
- 04-29注册下载啊
- 04-29Spring 中三种 BeanName 生成器!
- 04-29mysql学习9:创建数据库
- 04-29Linux之yum源详解
- 04-29夏日终曲/请以你的名字呼唤我/Call me by your name(无剧透)
- 04-29注释竟然还有特殊用途?一文解惑 //go:linkname 指令
- 最近发表
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- sqlset (59)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)