What is merge conflict in Git? How to handle merge conflicts?

GitGitHubOpen SourceSoftware & Coding

In real world, when we merge branches, we will run into conflicts quite often. Conflict happens because of the following reasons −

  • When the same line of code is changed in different ways in two branches.

  • A given file is changed in one branch but deleted in another branch.

  • Same file is added twice in two different branches but the content of the file is different.

In these cases, git will stop the merge process as it cannot figure out how to merge the changes. In such scenarios we need to intervene manually and instruct how to proceed with the merging process.

The diagram given below shows two branches master and feature. The file hello.txt, initially, contains a line Hello. After the initial commit, the branches have diverged as they have separate commits. Each commit has modified the same line in the file.

When we try to merge the changes from feature branch to the master branch, we will encounter a Merge Conflict error as shown in the second diagram. This is well understood that git will not be able to determine which commit to maintain as the second line is different in each commit. So, it will prompt us to confirm if we would like to keep only changes from the mater branch, or keep changes only from the feature branch or keep the changes from both feature and master branches.

Example

Let us see this in action to understand the merge conflict.

Step 1 − Create a repository with initial commit with the hello.txt file.

$ git init
Initialized empty Git repository in E:/tut_repo/.git/

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ echo hello>hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git add .

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git commit -m 'hello.txt'
[master (root-commit) b6a745d] hello.txt
1 file changed, 1 insertion(+)
create mode 100644 hello.txt

Step 2 − Create a new branch feature. Switch to the feature branch and create a new commit by editing the second line in the hello.txt file.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git branch feature

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git switch feature
Switched to branch 'feature'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ echo hello feature >> hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git add .

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git commit -m 'hello feature'
[feature a004fa4] hello feature
1 file changed, 1 insertion(+)

Step 3 − Now switch to the master branch and perform a new commit by adding a new line to hello.txt.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git switch master
Switched to branch 'master'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ cat hello.txt
hello

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ echo hello master>>hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git add .

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git commit -m 'hello master'
[master 6478b3e] hello master
1 file changed, 1 insertion(+)

Step 4 − We will now try to merge the changes from the feature branch to the master branch.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git merge feature
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master|MERGING)

The output shows that the branch is now in an intermediate state of merging as the automatic merging has failed due to the conflict. We would need to interfere manually to complete the merging process.

Step 5 − Check the contents of the file hello.txt

$ cat hello.txt
hello
<<<<<<< HEAD
hello master
=======
hello feature
>>>>>>> feature

From the contents of the file, it is clear that the first line is unchanged. The second line will be considered from both the branches. The output screen shows a divider with the "=======" notation. The first half of the divider contains the separator "<<<<<<< HEAD" which means that the contents are from the current branch where HEAD points to - master. The second half of the divider contains the separator ">>>>>>> feature" which means the contents are from the second branch - feature. Now we need to decide whether we need to keep the first half or the second half or both the changes.

Step 6 − We are deciding to keep both the changes so we modify the file manually by keeping only the contents and removing the divider "=======" notation, and the separators "<<<<<<< HEAD" and ">>>>>>> feature".

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master|MERGING)
$ cat hello.txt
hello
hello master
hello feature

Step 7 − Now commit the changes and display the history.

$ git add .

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master|MERGING)
$ git commit -m 'merge commit'
[master 1183ce0] merge commit

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline --all --graph
* 1183ce0 (HEAD -> master) merge commit
|\
| * a004fa4 (feature) hello feature
* | 6478b3e hello master
|/
* b6a745d hello.txt
raja
Published on 28-Apr-2021 14:09:27
Advertisements