合并冲突、解决冲突

本文详细介绍了在Git中遇到合并冲突时如何手动解决。通过切换分支,分别在master和feature分支上修改同一文件readme.txt,导致合并冲突。在Git提示冲突后,用户需要查看冲突文件,理解Git的冲突标记,并手动编辑文件以合并变化。解决冲突后,添加文件到暂存区并提交,完成合并。最后,删除已完成功能的feature分支。

合并冲突

在一个新分支上开发时,修改了一个文件内容,比如:

$ git checkout -b feature

Switched to a new branch ‘feature’

修改文件readme.txt最后一行,改为

Creating a new branch is quick AND simple.

然后在feature分支上提交

$ git add readme.txt

$ git commit -m “AND simple”

[feature 65f1845] AND simple

1 file changed, 1 insertion(+), 1 deletion(-)

然后再切换到master分支

$ git checkout master

Switched to branch ‘master’

修改文件readme.txt最后一行,改为

Creating a new branch is quick & simple.

然后在master分支上提交

$ git add readme.txt

$ git commit -m “& simple”

[master 38861b0] & simple

1 file changed, 1 insertion(+), 1 deletion(-)

现在,master分支和feature分支各自都分别有新的提交,变成了这样:
在这里插入图片描述
这种情况下,Git无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突

$ git merge feature

Auto-merging readme.txt

CONFLICT (content): Merge conflict in readme.txt

Automatic merge failed; fix conflicts and then commit the result.

Git告诉我们,readme.txt文件存在冲突,必须手动解决冲突后再提交

git status也可以告诉我们冲突的文件

$ git status

On branch master

You have unmerged paths.

(fix conflicts and run “git commit”)

Unmerged paths:

(use “git add …” to mark resolution)

   both modified:      readme.txt

no changes added to commit (use “git add” and/or “git commit -a”)

解决冲突

当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。

解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容,再提交。

查看一下冲突文件的内容

$ cat readme.txt

git is a distributed version control system.

git is free sofrware distributed under the GPL.

git is a mutable index called stage.

git tracks changes of files.

creating a new branch is quick and simple.

demi change something else.

<<<<<<< HEAD

creating a new branch is quick & simple.

=======

creating a new branch is quick AND simple .

feature

Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容

将文件内容修改如下后保存,及提交

Creating a new branch is quick and simple.

$ git add readme.txt

$ git commit -m “conflict fixed”

[master a3ca253] conflict fixed

现在master分支和feature1分支变成了下图所示:
在这里插入图片描述
用带参数的git log也可以看到分支的合并情况

$ git log --graph --pretty=oneline --abbrev-commit

  • a3ca253 conflict fixed

|\

| * 65f1845 and simple

  • | 38861b0 & simple

|/

  • 3fd20e0 branch test

  • db995d6 remove test file

  • 1b20f77 new test file

  • 9140c12 merge with no-ff

|\

| * c115606 add merge

|/

  • 382dccf conflict fixed

|\

| * 2be6792 and simple

  • | 5582737 & simple

|/

  • 79ecc03 branch test

  • 591bd0e remove test.txt

  • e3d8632 add test.txt

  • ac4cf6c git tracks changes twice

  • c7ef684 git tracks changes

  • a547464 understand how stage works

  • 95f796b append GPL

  • 6fd762a add distributed

  • 5cd1965 wrote a readme file

最后,删除feature分支,工作完成

$ git branch -d feature

Deleted branch feature (was 65f1845).

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Monster_起飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值