How to Resolve Merge Conflicts in Git

Azhar Bashir Khan Feb 02, 2024
How to Resolve Merge Conflicts in Git

In this tutorial, we will demonstrate how to resolve conflicts arising upon merging two branches in Git.

Typically, work is often done on the same files by different team members in a collaborative team environment. For example, one team member working on some feature development may edit the README file to provide information about the feature developed.

Another team member working on fixing bugs may add information on the bugs fixed in the same README file. They might be working on different branches and committing their changes in their respective branches.

From time to time, different branches need to be merged to provide a cohesive build. Thus, this operation will cause a merge conflict when the same files are updated in different branches.

One then needs to resolve the conflicts and merge the changes. We will now illustrate this with an example.

Resolving the Conflicts Arising Upon Merging of Two Branches With git mergetool

Before resolving merge conflicts, we should set up diff tool used by Git as follows.

$ git config merge.tool meld
$ git config merge.conflictstyle diff3
$ git config mergetool.prompt false

The above commands set meld as the default diff tool. Also, we have set up the conflictstyle to diff3 (i.e.); this sets the diff tool to show the common ancestor for both files (current branch one and the one in the branch to merge from).

To see the different supported diff tools, run the following command.

$ git mergetool --tool-help

Now, we begin with the example to demonstrate how to resolve conflicts. Let’s say we have two branches as follows.

$ git branch
* main
  feature1

The first branch is the main branch and the second one is a feature development branch named feature1.

We have a README.md file with contents as follows in the main branch.

$ cat README.md
# Upwork
Upwork projects

As shown above, we are currently in the main branch. Then, we switch to the feature1 branch.

$ git checkout feature1
Switched to branch 'feature1'

$ git branch
  main
* feature1

We update the README.md and print its contents as follows.

$ cat README.md
This is conflicting branch line.

Now, we will merge the main branch with the feature1 branch to get the latest changes in that branch.

While merging, Git shows the merge conflicts as follows.

$ git merge main
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

We can get further information on the conflict as follows.

$ git status
On branch feature1
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both added:      README.md

no changes added to commit (use "git add" and/or "git commit -a")

We will now print the contents of the file README.md, which has the conflict.

$ cat README.md 
<<<<<<< HEAD
This is conflicting branch line.

||||||| merged common ancestors
=======
# Upwork
Upwork projects
>>>>>>> main

The file shows various symbols now.

And the symbols <<<<<<< followed by HEAD is an alias for the current branch. This indicates the beginning of the edits within this section.

The symbols ======= show the end of the revisions within the current branch and the beginning of the edits within a new one.

The symbols >>>>>>> followed by the remote branch name viz. main, shows where the attempted merge happened.

Now, we will use mergetool to resolve the conflicts.

$ git mergetool
Merging:
README.md

Normal merge conflict for 'README.md':
  {local}: created file
  {remote}: created file

This will launch meld (as we have set this as the default diff tool). Please see the image below.

git-mergetool-meld1

The left pane shows the edits to the file README.md done in the local branch. The middle pane contains the result of the changes done to resolve the conflict. The right pane shows the edits done in the remote branch viz. main (i.e.) the branch we want to merge.

We can choose to keep both the local and remote changes or either one of them. We will choose to keep the remote branch changes as follows.

git-mergetool-meld2

We will then save and exit the meld tool.

We will print the README.md file and see the update.

$ cat README.md
# Upwork
Upwork projects

We will now commit the changes to Git.

$ git commit -m "merged from main"
[feature1 3c39d7b] merged from main

We will now run the git diff command to check for conflicts between feature1 and main branches.

$ git diff feature1 main

We will check the status of the feature1 branch.

$ git status
On branch feature1

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	README.md.orig

It shows one file, README.md.orig, which was created by the mergetool while merging.

Run the following command to remove it:

$ git clean -f

Thus, we have successfully resolved conflicts when merging two branches using the mergetool of Git.

Related Article - Git Merge