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,
Hello Git..!
First Line
Committed as the initial commit.
(abc) -> branch
Hello Git..!
First Line
Second Line
Committed.
(master) -> branch
Hi Git..!
First Line
Committed.
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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With