Git 解决冲突


抛出问题

新建一个分支 A,在分支 A 上的 readme.txt 文件的最后添加 AAA;然后切换到分支 B,在分支 B 上的 readme.txt 文件的最后添加 BBB。然后合并,就会发生冲突了。

解决问题

创建新的 daodesign 分支:

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

修改 readme.txt 最后一行,改为:

http://www.imdao.com

在 daodesign 分支上提交:

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

切换到 master 分支:

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

Git 还会自动提示我们当前 master 分支比远程的 master 分支要超前1个提交。
在 master 分支上把 readme.txt 文件的最后一行改为:

ftp://daodesign.net

提交:

$ git add readme.txt
$ git commit -m "master update1"
[master 470084b] master update1
 1 file changed, 1 insertion(+), 1 deletion(-)

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

$ git merge daodesign
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 and 'origin/master' have diverged,
and have 2 and 1 different commit each, respectively.
  (use "git pull" to merge the remote branch into yours)
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")

直接双击打开查看 readme.txt 文件的内容或在 git Bash 上查看:

$ git diff
diff --cc readme.txt
index de393bf,e94f281..0000000
--- a/readme.txt
+++ b/readme.txt
@@@ -2,4 -2,4 +2,8 @@@ Git is a distributed version control sy
  Git is free software distributed under the GPL.
  Git has a mutable index called stage.
  Git tracks changes of files.
- ftp://daodesign.net
 -http://www.imdao.com
++<<<<<<>>>>>> daodesign

Git 用<<<<<<<=======>>>>>>>标记出不同分支的内容。
修改 readme.txt 后保存:

daodesign.net

再提交:

$ git add readme.txt
$ git commit -m "update1"
[master 99876b7] update1

现在,master 分支和 daodesign 分支变成了下图所示:
Git 解决冲突
用带参数的git log也可以看到分支的合并情况:
$ git log --graph --pretty=oneline --abbrev-commit

$ git log --graph --pretty=oneline --abbrev-commit
*   99876b7 update1
|
| * 061e7e9 daodesign update1
* | 470084b master update1
|/
* 75c6cdb branch readme.txt
* d57f9f8 add text.txt
* bc94e10 readme-5
* 8c5c878 readme.txt-5
* 75dd30c readme.txt-4 and license-1
* 10412e5 third
* a85bbe5 second add
* 070953a readme - first

最后,删除 daodesign 分支:

$ git branch -d daodesign
Deleted branch daodesign (was 061e7e9).

git log --graph命令

$ git log --graph

$ git log --graph
*   commit 99876b7d4bf173167bb055e795a1ab0a128cc4d0
|  Merge: 470084b 061e7e9
| | Author: chan 
| | Date:   Wed Mar 8 23:02:57 2017 +0800
| |
| |     update1
| |
| * commit 061e7e9092a1cdd0a22bf9b8c8b698fe63e293d7
| | Author: chan 
| | Date:   Wed Mar 8 22:56:04 2017 +0800
| |
| |     daodesign update1
| |
* | commit 470084b0e5015c16eeb4d175f0d10df37336d5e3
|/  Author: chan 
|   Date:   Wed Mar 8 22:58:11 2017 +0800
|
|       master update1
|
* commit 75c6cdbb83b6f5f71a2bd5ff3eadb826ceae4a1e
| Author: chan 
| Date:   Wed Mar 8 22:22:24 2017 +0800
|

$ git log --graph --pretty=oneline

$ git log --graph --pretty=oneline
*   99876b7d4bf173167bb055e795a1ab0a128cc4d0 update1
|
| * 061e7e9092a1cdd0a22bf9b8c8b698fe63e293d7 daodesign update1
* | 470084b0e5015c16eeb4d175f0d10df37336d5e3 master update1
|/
* 75c6cdbb83b6f5f71a2bd5ff3eadb826ceae4a1e branch readme.txt
* d57f9f87a6ac83d4cf9a2f2bb269a6ff00b64826 add text.txt
* bc94e10595cd9723b5b46669a035ded58afd542f readme-5
* 8c5c87823b88e199ebf1b10efaacbce12dc534d1 readme.txt-5
* 75dd30cb2234f39e05bcf3820054cb950137f646 readme.txt-4 and license-1
* 10412e541963a0b4e93679b0eb572668ff485b2f third
* a85bbe59694726126449456b8e7f6e96f3af4f60 second add
* 070953a7e301f79eb6c57a325fc870d545fb17dd readme - first

总结

当 Git 无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。
git log --graph命令可以看到分支合并图。


<< Git 分支管理-创建与合并分支 Git 分支管理策略 >>
  • 作者 建站迷

    建站迷

    让天下没有难做的网站!
    解决中小型企业的无站之伤。



没有账号? 忘记密码?

社交账号快速登录