how does exactly a git merge conflict happen?

2019-03-08 14:02发布

I have made a git repository and added a text file to it. This is 100% for learning purpose.

  1. I added "1" to the text file and committed it to master.

  2. Created a new branch from master and appended "2".

  3. Finally, created a branch from master and appended "3".

Could you please explain how a conflict may occur in this, or any other, scenario?

2条回答
放荡不羁爱自由
2楼-- · 2019-03-08 14:47

A merge conflict happens when two branches both modify the same region of a file and are subsequently merged. Git can't know which of the changes to keep, and thus needs human intervention to resolve the conflict.

In this case, your steps 2 and 3 create two branches that have conflicting changes.

查看更多
放荡不羁爱自由
3楼-- · 2019-03-08 15:01

You will have a conflict if you merge:

  • branch2 to master (no conflict)
  • branch3 to master (conflict):

That is because:

  • The common ancestor would be master (with a second line empty)
  • the source content is branch3 (with a second line including "3")
  • the destination content is on latest of master (with a second line including "2", from the merge of branch2 to master)

Git will ask you to choose which content to keep ("3", "2", or both).

First, do the merges after:

git config merge.conflictstyle diff3

See "Fix merge conflicts in Git?".

查看更多
登录 后发表回答