Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this merge produce a conflict

Tags:

git

When I run this command:

 git log HEAD..other_branch -- some_file.txt

There's no output at all which I assume meants that there were no changes to some_file.txt in other_branch. Yet when I run git merge other_branch I get a whole slew of conflicts in some_file.txt.

When I run:

git log HEAD...other_branch -- some_file.txt 

I get two commits. One where the file was modified and one where the branch where it was modified was merged into HEAD.

I assumed that because the file was only changed in one branch that there wouldn't be any conflicts. Why are there conflicts? Is there a way to see what will conflict (and why) before I run git merge?

like image 624
Tyrone Slothrop Avatar asked May 02 '11 03:05

Tyrone Slothrop


People also ask

Why does a merge conflict occur?

Often, merge conflicts happen when people make different changes to the same line of the same file, or when one person edits a file and another person deletes the same file. You must resolve all merge conflicts before you can merge a pull request on GitHub.

What is merging conflicts?

A merge conflict is an event that takes place when Git is unable to automatically resolve differences in code between two commits. Git can merge the changes automatically only if the commits are on different lines or branches.

How do you identify a merge conflict?

To see the beginning of the merge conflict in your file, search the file for the conflict marker <<<<<<< . When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <<<<<<< HEAD .


1 Answers

To add to manojlds's contribution:

Very nice. But then the following should accomplish the same?

git diff master:some_file.txt someBranch:some_file.txt

that way you don't need to checkout before diffing against cached


Original answer:

The command

git log HEAD..other_branch -- some_file.txt

is identical to

git log ^HEAD other_branch -- some_file.txt

which means give me the log of all commits reachable from other_branch but NOT reachable from HEAD for some_file.txt. If this command gives you no output, it means that some_file.txt has not changed on other_branch at all.

On the other hand:

git log HEAD...other_branch -- some_file.txt 

is the symmetric difference between HEAD and other_branch, i.e. the commits that are in HEAD and in other_branch but not in both, and are the commits that will be merged when you merge the two branches. So something has probably happened to some_file.txt on HEAD that cause this conflict with the version on other_branch

like image 68
ralphtheninja Avatar answered Nov 10 '22 02:11

ralphtheninja