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

来源:互联网 发布:高起专网络教育总学分 编辑:程序博客网 时间:2024/05/22 03:32

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

首先我们创建一个分支:

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

$ cat readme.txtHello 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 masterSwitched to branch 'master'Your branch is up-to-date with 'origin/master'.$ cat readme.txtHello 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 conflictbranchAuto-merging readme.txtCONFLICT (content): Merge conflict in readme.txtAutomatic merge failed; fix conflicts and then commit the result.
当我们查看状态的时候:

$ git statusOn branch masterYour 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.txtno changes added to commit (use "git add" and/or "git commit -a")
原来是都修改了readme.txt文件。

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

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

$ cat readme.txtHello 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 conflictbranchDeleted branch conflictbranch (was 4c6d3a3).


0 0
原创粉丝点击