Skip to content

How to Resolve Merge Conflicts

Johnny Wu edited this page Mar 24, 2023 · 17 revisions

What is a merge conflict?

Version control systems like Git are all about managing contributions between multiple distributed authors (usually developers). Sometimes multiple developers may try to edit the same content. If Developer A tries to edit code that Developer B is editing a conflict may occur. To alleviate the occurrence of conflicts developers will work in separate isolated branches. The git merge command's primary responsibility is to combine separate branches and resolve any conflicting edits.

Understanding merge conflicts

Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it. In these cases, Git cannot automatically determine what is correct. Conflicts only affect the developer conducting the merge, the rest of the team is unaware of the conflict. Git will mark the file as being conflicted and halt the merging process. It is then the developers' responsibility to resolve the conflict.


Types of merge conflicts

There are 2 ways in which git merge (or a git pull, which is a git fetch and then a git merge) can fail:

  • Git fails to start the merge

    A merge will fail to start when Git sees there are changes in either the working directory or staging area of the current project. Git fails to start the merge because these pending changes could be written over by the commits that are being merged in. When this happens, it is not because of conflicts with other developer's, but conflicts with pending local changes. The local state will need to be stabilized using git stash, git checkout, git commit or git reset. You need to modify or stash the files it lists, and then try to do a git pull again. A merge failure on start will output the following error message:
    error: Entry '<fileName>' not uptodate. Cannot merge. (Changes in working directory)
    or
    error: Entry '<fileName>' would be overwritten by merge. Cannot merge. (Changes in staging area)
  • Git fails during the merge

    A failure DURING a merge indicates a conflict between the current local branch and the branch being merged. This indicates a conflict with another developers commited changes. Git will do its best to merge the files but will leave things for you to resolve manually in the conflicted files. A mid-merge failure will output the following error message:
    CONFLICT (content): Merge conflict in <fileName>
    Automatic merge failed; fix conflicts and then commit the result.

Clone this wiki locally