git---分支合并冲突处理(conflict)

本文介绍了如何处理Git分支合并时的冲突,包括通过merge request或手动合并,并详细讲解了如何将git diff设置为vimdiff进行差异比较,以及列举了一些常用的分支和tag管理命令。
摘要由CSDN通过智能技术生成

1 冲突处理

基于同个原始分支后,两个分支同步开发,最后合并:

这里写图片描述

合并两个分支
# branch1/2可以是分支名,也可以通过指定版本的SHA1 16进制的前7个位
git merge branch1 branch2
解决冲突

  通过git diff来比较两个分支的不同点,手动合并:

# 比较两个分支(默认比较所有的不同)
git diff d02b6ff 919b8ea

# 比较版本d02b6ff和版本919b8ea的common.h文件
# 默认diff
git diff d02b6ff 919b8ea -- api/include/common.h
# 配置好vim diff
git d d02b6ff 919b8e
当我们在Git合并分支时,如果两个分支中的同一文件的同一部分被修改了,就会发生冲突。此时,Git会提示我们手动解决冲突。以下是解决冲突的步骤: 1.使用git merge命令合并分支,如果发生冲突Git会在命令行中输出一些描述信息,以便让我们知道有冲突发生。 2.使用git status命令查看冲突的详情,Git会列出所有未合并的文件。 3.打开冲突文件,手动解决冲突Git会在冲突文件中用<<<<<<<,=======和>>>>>>>标记出不同分支的内容。我们需要根据需要修改文件,然后删除这些标记。 4.使用git add命令将修改后的文件标记为已解决冲突。 5.使用git commit命令提交合并结果。 以下是一个解决冲突的例子: ```shell $ git merge feature Auto-merging merge.txt CONFLICT (content): Merge conflict in merge.txt Automatic merge failed; fix conflicts and then commit the result. $ git status On branch main 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: merge.txt no changes added to commit (use "git add" and/or "git commit -a") $ cat merge.txt This is a line in the main branch. <<<<<<< HEAD This is a line in the feature branch. ======= This is another line in the main branch. >>>>>>> feature $ vim merge.txt $ cat merge.txt This is a line in the main branch. This is a line in the feature branch. This is another line in the main branch. $ git add merge.txt $ git commit -m "Merge feature branch" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值