創(chuàng)建一個新分支:
$ git checkout -b feature1
Switched to a new branch 'feature1'
修改 readme.txt 最后一行政溃,改為:
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 分支:
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
Git 還會自動提示我們當前 master 分支比遠程的 master 分支要超前1個提交无宿。
在 master 分支上把 readme.txt 文件的最后一行改為:
Creating a new branch is quick & simple.
提交:
$ git add readme.txt
$ git commit -m "& simple"
[master 400b400] & simple
1 file changed, 1 insertion(+), 1 deletion(-)
現(xiàn)在,master 分支和 feature1 分支各自都分別有新的提交。這種情況下望拖, Git 無法執(zhí)行“快速合并”,只能試圖把各自的修改合并起來,但這種合并就可能會有沖突银伟,我們試試看:
$ 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 的內(nèi)容:
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 用 <<<<<<<夯辖,=======,>>>>>>> 標記出不同分支的內(nèi)容董饰,我們修改如下后保存:
Creating a new branch is quick and simple.
再提交:
$ git add readme.txt
$ git commit -m "conflict fixed"
[master 59bc1cb] conflict fixed