用到的新命令
1.git log --graph:查看到分支合并图
1.准备新的feature1分支,修改readme.txt
[root@VM_0_11_centos learn_git]# git checkout -b feature1
Switched to a new branch 'feature1'
[root@VM_0_11_centos learn_git]# git branch
* feature1
master
[root@VM_0_11_centos learn_git]# cat 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.
Creating a new branch is quick and simple.<==新加 and simple
2.add 和 commit
[root@VM_0_11_centos learn_git]# git add readme.txt
[root@VM_0_11_centos learn_git]# git commit -m "and simple"
[feature1 328da23] and simple
1 file changed, 1 insertion(+), 1 deletion(-)
3.切换到master,并修改readme.txt,add 和 commit,现在分支情况如图1
[root@VM_0_11_centos learn_git]# 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)
[root@VM_0_11_centos learn_git]# vim readme.txt
..省略..
Creating a new branch is quick & simple.<==改成 & simple
[root@VM_0_11_centos learn_git]# git add readme.txt
[root@VM_0_11_centos learn_git]# git commit -m "& simple"
[master 66fe1ee] & simple
1 file changed, 1 insertion(+), 1 deletion(-)
4.合并feature1,出错
[root@VM_0_11_centos learn_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.<== 失败,readme.txt文件存在冲突
[root@VM_0_11_centos learn_git]# 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")
#
# 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")
5.cat 查看冲突的readme.txt文件
<== git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,我们修改如下后保存:
[root@VM_0_11_centos learn_git]# cat 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.
<<<<<<< HEAD
Creating a new branch is quick & simple.<==makter分支
=======
Creating a new branch is quick and simple.<==feature1分支
>>>>>>> feature1
6.修改readme.txt,add 和 commit,现在分支情况如图2
[root@VM_0_11_centos learn_git]# vim readme.txt <==将5679行删除
[root@VM_0_11_centos learn_git]# cat 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.
Creating a new branch is quick and simple.
[root@VM_0_11_centos learn_git]# git add readme.txt
[root@VM_0_11_centos learn_git]# git commit -m "conflict fixed"
[master dc82b39] conflict fixed
7.用带参数的git log也可以看到分支的合并情,删除feature1分支
[root@VM_0_11_centos learn_git]# git log --graph --pretty=oneline --abbrev-commit
* dc82b39 conflict fixed
|\
| * 328da23 and simple
* | 66fe1ee & simple
|/
* 1d24cf9 branch test
* c0ef94e modify text.txt
* 4ab772a add text.txt
* f781044 delete test.txt
* 0142aa9 add test.txt
* 723e215 add reacks changes
* e45a7a3 understand how stage works
* 95a6a5b distributed under the GPL
* c0dafc1 add distributed
* ee6982d create a readme.txt
[root@VM_0_11_centos learn_git]# git branch -d feature1
Deleted branch feature1 (was 328da23).
当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。
解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容,再提交
图1.
图2.
来源:CSDN
作者:ztq小天
链接:https://blog.csdn.net/weixin_38850997/article/details/89513837