Git merge conflicts | Atlassian Git Tutorial

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


Merge Conflicts

Merge and conflicts are a common part of the Git experience. Conflicts in other version control tools like SVN can be costly and time-consuming. Git makes merging a breeze. Most of the time, Git will figure out how to automatically integrate new changes.

Conflicts usually arise when two people have changed the same lines in a file, or if a 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 performing the merge, the rest of the team is not aware of the conflict. Git will mark the file as conflicting and stop the merge process. It is then the responsibility of the developers to resolve the conflict.

Types of Merge Conflicts

A merge can enter a conflicting state at two separate points. When starting and during a merging process. The following is a discussion of how to address each of these conflict scenarios.

Git cannot start merge

A merge will not start when Git sees changes to the working directory or staging area of the current project. Git cannot start merging because these pending changes could be written by commits being merged. When this happens, it is not due to conflicts with other developers, but conflicts with pending local changes. The local state must be stabilized using git stash, git checkout, git commit, or git reset. A merge error at startup will generate the following error message:

Git fails during


An error DURING a merge indicates a conflict between the current local branch and the branch being merged. This indicates a conflict with another developer’s code. Git will do its best to merge the files, but will leave things for you to manually resolve into the conflicting files. A mid-merge error will generate the following error message:


a Merge

Conflict To really familiarize yourself with merge conflicts, the next section will simulate a conflict to examine and resolve later. The sample will use a Unix-like command-line Git interface to run the sample simulation.

This code example runs a script that accomplishes the following.

Create a new directory

  • named git-merge-test, change to that directory, and initialize it as a new Git repository
  • .

  • Create a new combination of text files.txt with some content
  • .

  • Add merge.txt to the repository and commit.

We now have a new repository with a parent branch and a combination of files.txt with content in it. Next, we’ll create a new branch to use as the conflicting merge.


continuation script accomplishes the following

: create and check out a new branch named new_branch_to_merge_later overwrite the content in the merge.txt commit the new

  • content With this new branch: new_branch_to_merge_later we have created a commit that

  • overrides the contents
  • of the

  • merge.txt

This command string verifies the parent branch, appends content to merge.txt and confirms it. This now puts our example repository in a state where we have 2 new commits. One in the main branch and one in the new_branch_to_merge_later branch. Right now let’s merge git new_branch_to_merge_later and see what happens!

BOOM 💥 . A conflict appears. Thank you, Git for letting us know about this!

How to identify merge conflicts

As we have experienced in the procedural example, Git will produce some descriptive result that will let us know that a CONFLICT has occurred. We can get more information by running the git status command

The output of the git state indicates that there are unmerged paths due to a conflict. The merge.text file now appears in a modified state. Let’s examine the file and see what gets modified.

Here we have used the cat command to publish the contents of the merge.txt file. We can see some strange new additions

  • <<<<<<< HEAD
  • =====

  • =
  • ==

  • >>>>>>> new_branch_to_merge_later

Think of these new lines as “conflict dividers”. The line ======= is the “center” of the conflict. All content between the center and the HEAD line is <<<<<<< content that exists in the current parent branch that the HEAD reference points to. Alternatively, all content between the center and the >>>>>>> new_branch_to_merge_later is content that is present in our fusion branch.

How to resolve merge conflicts

by using the command line

The most direct way to resolve a merge conflict is to edit the conflicting file. Open the merge.txt file in your favorite editor. For our example, let’s simply remove all conflict dividers. The content .txt modified merge should look like this:

Once the file has been edited, use git add merge.txt to organize the new merged content. To finish

the merge, create a new commit by running: Git will see that the conflict

has been resolved and create a new merge commit to end the merge


Git commands that can help resolve

merge conflicts

General tools

The status command is frequently used when working with Git and during

a merge will help identify conflicting files.

Passing the -merge argument to the git log command will produce a log with a list of conflicting commits between merge branches.

diff helps to find differences between the states of a repository/files. This is useful for predicting and preventing merge conflicts.

Tools for when git cannot initiate

a merge checkout can be used to undo changes to

files, or to change branch reset can be used to undo changes to

the working directory and staging area


Tools for when git conflicts arise

during a merge Running the

git merge with the -abort option will exit the merge process and return the branch to the state before the merge began. Git


can be used during a merge conflict to reset conflicting files to a state that is well known


Combination conflicts can be an intimidating experience. Fortunately, Git offers powerful tools to help navigate and resolve conflicts. Git can handle most merges on its own with auto-merge features. A conflict arises when two separate branches have made edits on the same line in a file, or when a file has been deleted in one branch but edited in the other. Conflicts are most likely to occur when working in a team environment.

There are many tools to help resolve merge conflicts. Git has many command-line tools that we discuss here. For more detailed information about these tools, visit separate pages for git log, git reset, git status, git checkout, and git reset. In addition to Git, many third-party tools offer optimized merge conflict support features.