Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't git give me any conflicts?

I found a strange problem. I was testing a git repo and found in this situation.

I created a simple git repo and add a text file with the following content.

Hello Git..!

First Line

Committed as the initial commit.

Then I created a branch called "abc" from that commit and change the content of the file to below.

(abc) -> branch

Hello Git..!

First Line
Second Line

Committed. Then I checked out back to my master branch and change the file content to below.

(master) -> branch

Hi Git..!

First Line

Committed. Then I tried to merge "abc" branch into "master" branch. I thought it would give me a conflict, but it had successfully merged via recursively to below.

Hi Git..!

First Line
Second Line

What didn't it show any conflicts? I tried with several attempts sometimes if there is no empty line between like below,

Hello Git...!
First Line

then it shows that there is a conflict. I can't understand this. Someone please help me to understand this.

I searched as much as I can but I could not able to find a question or answer to solve my question.

I have uploaded the sample repo to GitHub. If someone clones it and try to merge, it can be reproduced.

https://github.com/Ranjith-Suranga/test-repo

Is there any way to force GIT to show conflicts? I know it is a strange question, no one needs conflicts to arise. But I don't like this happening too. Just imagine, this happens to my real code.

Update


Since most of the comments and answers are suggesting that there can not have any conflict like this. I had to update this answer.

Okay, let's go all of these above steps once again after removing the second empty line.

Here we go,

  1. I created a simple git repo and add a text file with the following content. This time around no empty line between the first and second line.
Hello Git..!
First Line

Committed as the initial commit.

  1. Then I created another branch called "abc" from that commit and change the content of the file to below. (again removing the empty line)

(abc) -> branch

Hello Git..!
First Line
Second Line

Committed.

  1. Then I checked out back to my master branch and change the file content to below.

(master) -> branch

Hi Git..!
First Line

Committed.

  1. Then I tried to merge "abc" branch into "master" branch. This time around it is going to say me there is a merge conflict.

If you don't trust me, here is the sample github repo https://github.com/Ranjith-Suranga/test-repo2

The first one doesn't give any conflict, the second one almost the same except for that removed empty line, it gives conflict. But the question is why?

like image 598
Ranjith Suranga Avatar asked May 15 '19 15:05

Ranjith Suranga


People also ask

How do I see conflicts in Git?

Git can handle most merges on its own with automatic merging features. A conflict arises when two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other. Conflicts will most likely happen when working in a team environment.

How do I create a Git conflict?

Edit the same line in two branches, and try to merge Merge conflicts in git happen, when two branches were changed on the same line or in the same content of a file before a merge. If you just extend a file or append something, git usually just figures it out by itself.

How do I enable Resolve Conflicts button in GitHub?

For more information, see "About protected branches." Under your repository name, click Pull requests. In the "Pull Requests" list, click the pull request with a merge conflict that you'd like to resolve. Near the bottom of your pull request, click Resolve conflicts.


1 Answers

Notice that all versions of your file contain an incomplete last line. When you add another line, this means that you not just add a line, but you also remove the incomplete line and then add two lines.

In your second try, when you merge the two branches, Git detects changes in adjacent blocks of text. Contrary to what you might think, the line reading

First Line

does in fact not remain unchanged because the final line break that is added in one branch counts as a modification of that line. Since you have modifications in adjacent lines coming from different branches, Git reports a merge conflict.

In your first try, have a blank line above the line reading First Line. This line acts as an anchor point that remains stable during the merge, so to say. It allows Git to clearly separate the changes coming from different branches into different blocks of text. Therefore, Git can consolidate the modifications automatically, without a conflict.

like image 100
j6t Avatar answered Oct 06 '22 01:10

j6t