Search code examples
gitmergeconflict

Git does not detect a conflict in code on same line


I am learning git. I have the tried the following example: in the master branch I created a file 1.txt that has one line hi. I then switched to a new branch "mybranch" and edited the same line to hello instead of hi

When I get back to the master branch and run a git merge command git merge mybranch its doesn't detect a conflict that I should resolve, instead it replaces the word hi with hello. Essentially it replaces some code in master branch without letting me know.

My questions is why didn't git consider that to be a conflict even tho the bit bucket website says

A conflict arises when two separate branches have made edits to the same line in a file


Solution

  • The way a merge works in Git, ignoring renames, is that Git considers three points in the history: the heads of the two branches, and the merge base, which is usually the most recent common ancestor. Git merges by looking at the changes between the merge base and each head (usually a branch).

    If one side contains a change, such as on mybranch, but the other does not, such as master, then the result of the merge is the change. This is the goal of why one performs a merge in the first place. If both sides have identical changes or neither side has a change, then Git takes the version that's common. A conflict occurs only if both sides have different changes in the same area, since Git then doesn't know what to do.