使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作
创建分支
git branch (branchname)
切换分支
git checkout (branchname)
列出分支
git branch
我们也可以使用 git checkout -b (branchname) 命令来创建新分支并立即切换到该分支下
git checkout -b test
删除分支
git branch -d (branchname)
分支合并
某分支有了独立内容,你会希望将它合并回到你的主分支
git merge test
合并冲突
$ git merge change_site
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
$ cat test.txt
<<<<<<< HEAD
runoob.com
新增加一行
=======
www.runoob.com
>>>>>>> change_site
我们将一个分支合并到 "master" 分支,一个合并冲突就出现了,接下来我们需要手动去修改它
$ vim test.txt
$ cat test.txt
www.runoob.com
新增加一行
$ git diff
diff --cc test.txt
index f84c2a4,bccb7c2..0000000
--- a/test.txt
+++ b/test.txt
@@@ -1,2 -1,1 +1,2 @@@
- runoob.com
+ www.runoob.com
+新增加一行
在 Git 中,我们可以用 git add 要告诉 Git 文件冲突已经解决
$ git status -s
UU test.txt
$ git add test.txt
$ git status -s
M test.txt
$ git commit
[master 88afe0e] Merge branch 'change_site'
现在我们成功解决了合并中的冲突,并提交了结果。