windows下git的安装、配置与操作(八)——git冲突的解决

对分支进行合并的时候,有可能会产生冲突。冲突的解决就是要修改冲突的内容。

首先我们创建一个分支:

$ git checkout -b conflictbranch
Switched to a new branch 'conflictbranch'
我们修改文件的最后一行:

$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
Creating a new branch is quick and simple.
然后添加提交:
$ git add readme.txt
$ git commit -m "and simple."
[conflictbranch 4c6d3a3] and simple.
 1 file changed, 1 insertion(+), 1 deletion(-)
这时候我们回到master分支并对文件进行修改:

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
Creating a new branch is quick & simple.
然后我们在master分支提交:

$ git add readme.txt
$ git commit -m "& simple."
[master 554d2c8] & simple.
 1 file changed, 1 insertion(+), 1 deletion(-)
当我们试图将conflictbranch合并到master的时候,发现这里起了冲突:

$ git merge conflictbranch
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
当我们查看状态的时候:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")
原来是都修改了readme.txt文件。

我们查看文件的内容发现了冲突的位置:

$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick and simple.
>>>>>>> conflictbranch
我们可以直接对文件内容进行修改:

$ cat readme.txt
Hello World!
this is a git file.
this file will commit to git.
we are young!yes we are young!
Creating a new branch is quick and simple.
然后再提交:

$ git add readme.txt
$ git commit -m "conflict fixed"
[master a6791d5] conflict fixed
通过日志可以看到详细的合并过程:

$ git log --pretty=oneline --graph -4
*   a6791d55e3526f4c9f6f137a21801af61a13d3e2 conflict fixed
|\
| * 4c6d3a3aaa89f1d9954340af0cb2641f3f95dba7 and simple.
* | 554d2c80016370b6e88a596f12fc282a3d820e8a & simple.
|/
* ecf03eb78643b6b7eafcc9e27848d8eba51462c6 branch dev modify
最后就可以删除分支conflictbranch:

$ git branch -d conflictbranch
Deleted branch conflictbranch (was 4c6d3a3).


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值