Git学习日志--解决冲突

程序员成长之旅 · 程序员成长之旅/Linux学习 · 582 字

解决冲突

在合并的时候不总是一帆风顺,出现冲突的时候就要手动解决冲突后再提交

#创建冲突项目
git checkout -b featurel

修改原有文件的最后一行为Creating a new branch is quick AND simple.

提交

git add README.md
git commit -m "add type AND simple"

切回master并且修改最后一行

git checkout master
vim README.md
---修改最后一行内容为Creating a new branch is quick & simple. ---
git add README.md 
git commit -m "add type & simple."

现在,master分支和feature1分支各自都分别有新的提交,变成了这样:

git-br-feature1

git merge feature1 #尝试合并feature1
返回提示
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt  #冲突(内容):readme.txt中的合并冲突
Automatic merge failed; fix conflicts and then commit the result. #自动合并失败;修复冲突,然后提交结果。
#使用git status 也可以提示冲突
git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits. #你的分支在2次提交之前领先于'origin/master'
  (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")

解决冲突

cat README.md #查看内容
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用<<<<<<<,=======,>>>>>>>标记出不同分支的内容

将冲突内容删除/修改后最终得到以下内容

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.
Creating a new branch is quick and simple.

现在,master分支和feature1分支变成了下图所示:

git-br-conflict-merged

#使用git log 可以查看分支合并的情况
root@izbp11iqplyxdovikmbnimz ~/testClone# git log --graph --pretty=oneline --abbrev-commit
*   ee2cfde file write type and simple.
|\  
| * 54636c0 add type AND simple
* | 0e34273 add type & simple
|/  
* b3e0f86 add a type
* a5f9462 add an type
* de38bf3 Initial commit

用git log --graph命令可以看到分支合并图。