人生不如意之事十之八九,合併分支每每也不是一路順風。git
準備新的feature1分支,繼續咱們的新分支開發:ui
$ git checkout -b feature1
Switched to a new branch 'feature1'spa
修改readme.txt最後一行,改成:3d
Creating a new branch is quick AND simple.blog
在feature1分支上提交:開發
$ git add readme.txtit
$ git commit -m "AND simple"
[feature1 ab1c693] AND simple
1 file changed, 1 insertion(+), 1 deletion(-)io
切換到master分支:table
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)ast
Git還會自動提示咱們當前master分支比遠程的master分支要超前1個提交。
在master分支上把readme.txt文件的最後一行改成:
Creating a new branch is quick & simple.
提交:
$ git add readme.txt
$ git commit -m "& simple"
[master ea364c9] & simple
1 file changed, 1 insertion(+), 1 deletion(-)
如今,master分支和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.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
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的內容:
Git is a distributed version control system
Git is free sofwore 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 "confict fixed"
[master 20864c0] confict fixed
如今,master分支和feature1分支就變成了下圖所示的:
用帶參數的git log也能夠看到分支的合併狀況:
$ git log --graph --pretty=oneline --abbrev-commit
* 20864c0 (HEAD -> master) confict fixed
|\
| * ab1c693 (feature1) AND simple
* | ea364c9 & simple
|/
* 3d9a6be branch test
* d255aa7 (origin/master) LICENSE
* 4e28fb6 readme.txt
最後,刪除 feature1分支:
$ git branch -d feature1
Deleted branch feature1 (was ab1c693).
工做完成了
摘抄自:
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001375840202368c74be33fbd884e71b570f2cc3c0d1dcf000