git解决冲突

原文见:廖雪峰的官方博客。html

http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001375840202368c74be33fbd884e71b570f2cc3c0d1dcf000html5

 

人生不如意之事十之八九,合并分支每每也不是一路顺风的。git

准备新的feature1分支,继续咱们的新分支开发:app

$ git checkout -b feature1
Switched to a new branch 'feature1'

修改readme.txt最后一行,改成:ide

Creating a new branch is quick AND simple.

feature1分支上提交:测试

$ git add readme.txt 
$ git commit -m "AND simple"
[feature1 75a857c] AND simple
 1 file changed, 1 insertion(+), 1 deletion(-)

切换到master分支:ui

$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.

Git还会自动提示咱们当前master分支比远程的master分支要超前1个提交。spa

master分支上把readme.txt文件的最后一行改成:code

Creating a new branch is quick & simple.

提交:htm

$ git add readme.txt 
$ git commit -m "& simple"
[master 400b400] & simple
 1 file changed, 1 insertion(+), 1 deletion(-)

如今,master分支和feature1分支各自都分别有新的提交,变成了这样:

git-br-feature1

这种状况下,Git没法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,咱们试试看:

$ git merge feature1
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
# Your branch is ahead of 'origin/master' by 2 commits.
#
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#       both modified:      readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

咱们能够直接查看readme.txt的内容:

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1

Git用<<<<<<<=======>>>>>>>标记出不一样分支的内容,咱们修改以下后保存:

Creating a new branch is quick and simple.

再提交:

$ git add readme.txt 
$ git commit -m "conflict fixed"
[master 59bc1cb] conflict fixed

如今,master分支和feature1分支变成了下图所示:

git-br-conflict-merged

用带参数的git log也能够看到分支的合并状况:

$ git log --graph --pretty=oneline --abbrev-commit
*   59bc1cb conflict fixed
|\
| * 75a857c AND simple
* | 400b400 & simple
|/
* fec145a branch test
...

如今,删除feature1分支:

$ git branch -d feature1
Deleted branch feature1 (was 75a857c).

工做完成。

 

 

 

小结

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

git log --graph命令能够看到分支合并图。

 

 

 

教程写成这样真是造福人类。

分支合并部分楼主只提到master先合并dev时对冲突进行手动修改而后提交。其中有几个问题不清楚实际测试了一下才知道。

1)出现冲突时,在master修改冲突并commit以前没法切换到dev分支。

2)master修改冲突并提交以后切换到dev,发现dev内容保持原来不变。

3)此时在dev下再次合并master无冲突,直接替换成master修改冲突并提交以后的内容。

dev先合并master的状况相同。

实验跟楼主在楼下总结的

“实际项目中master是主线,每一个开发小组都有本身的分支,好比dev-1, dev-2,每一个组都要干两件事:

1)把本身的dev-N往master merge:本身的修改提交到master。  (qs注:在master分支下进行git merge dev-N)

2)把master往本身的dev-N merge:得到别人的修改。”相吻合。  (qs注:在本身的dev-N下进行git merge master)

相关文章
相关标签/搜索