Updated April 13, 2023
Introduction to Resolve Merge Conflict in GIT
The following article provides an outline for Resolve Merge Conflict in GIT. Managing contributions to resolve conflicts among multiple distributed developers is one of version control systems’ main jobs. To resolve merge conflicts, we first need to understand what exactly it is. It arises during merging different versions of the same code and is managed by the version control system, mostly when a developer deletes a file while some other developer was still modifying it or when multiple people try to change the same line in a file. In this article, we’ll get to know more about Git merge conflicts, what causes them, and how they can be resolved.
What is Merge Conflict in GIT?
Git has made merging very easy as it automatically integrates new changes using the git merge command. But in case of conflicts, only the developer who is merging is aware of the conflict, while the others are unaware. So what Git does is halts the merging process and flags the file as having conflicts. The developer then finds and resolves the conflict.
Conflicts during merge can happen in the below-described ways:
- Failure to Start the Merge: In case of pending changes in the working directory of the current project, a merge can fail as they could be overwritten by the commits while being merged. But it can be stabilized using various git commands like git checkout/stash/reset/commit.
- Failure During Merging: If there’s a failure during merging, it indicates that there is a conflict between the branch that’s being merged and the current branch. Although Git tries to merge the files without conflicts, it’ll leave it up to you to manually resolve it if unable to do so.
How to a Create File Merge Conflict in GIT?
Let’s take an example to show/resolve merge conflict in GIT using the command line Git interface.
Code:
$ mkdir git-merge-test
$ cd git-merge-test
$ git init
$ echo "Mess with this content" > new_merged.txt
$ git add new_merged.txt
$ git commit -am"Committed the earlier content"
[master (root-commit) d58f73b] Committed the earlier content
1 file changed, 1 insertion(+)
create mode 200548 new_merged.txt
Let’s see line by line what’s happening in the above snippet of commands.
- Git-merge-test creates a new directory and initializes a new Git repository.
- Next, create a text file named new_merged.txt with something written on it.
- Add the newly made text to the repo and then commit it.
We now have a new repository with one master branch and a file new_merged.txt with some content in it. Then, we create another branch that will conflict with the merge.
Code:
$ git checkout -b newbranch
$ echo "New content" > new_merged.txt
$ git commit -am"Same named file is edited to cause a conflict"
[newbranch 6282319] Same named file is edited to cause a conflict
1 file changed, 1 insertion(+), 1 deletion(-)
To break it down to you, the above lines:
- creates and checks out a newbranch named newbranch.
- overwrites the content of the new_merged.txt file.
- And finally, it commits the newly created content.
With the newbranch named one branch, a commit is created that overwrites the content in new_merged.txt.
Code:
$ git checkout master
Switched to branch 'master'
$ echo "Append" >> new_merged.txt
$ git commit -am"Content appended new_merged.txt"
[master 24fbe3c] Content appended new_merged.txt
1 file changed, 1 insertion(+)
The above sequence of commands not only checks out the master branch, adds content to new_merged.txt but also commits it in the end. Now our example has 2 new commits. One is in the master branch, and there’s one more in the newbranch. Now git merge the newbranch to see what happens.
Code:
$ git merge newbranch
Auto-merging new_merged.txt
CONFLICT (content): Merge conflict in new_merged.txt
Automatic merge failed; fix conflicts and then commit the result.
Git shows us that conflict has appeared.
Resolving Merge Conflicts in GIT
Editing the conflicted file is one of the ways to remove a merge conflict. Just open the conflicted file in the editor and remove the conflict dividers one by one. Once edited, you can use git add <file_name> to add the modified content. A new commit can be created by executing:
Code:
git commit -m "merged and resolved the conflict in <file_name>"
Git ensures resolving the conflict and thus creates a new committed merge to finalize it. Some of the Git commands which help to resolve merge conflicts are:
Code:
git log --merge
The merge –abort passed with git log command produces a list of commits conflicting between merging branches.
Code:
git status
It helps to identify conflicted files.
Code:
git diff
It helps in finding differences among the states of a repository or a file. It is used to predict and prevent merge conflicts. Some tools like the ones given below are used when starting a merge fail.
Code:
git reset --mixed
It is used to undo the changes made to the current directory.
Code:
git checkout
It is used for undoing the changes to files, or more frequently, for changing branches. Some tools like the ones given below are used when conflicts arise during a merge,
git reset: It is used to reset conflicted files during a merge conflict.
Code:
git merge --abort
Passing –abort argument with git merge enables one to exit the merge process and return the branch to its original state.
Advantages of Resolving GIT Merge Conflicts
Given below are the advantages mentioned:
- It is used to create, modify, delete branches, files, or repositories without conflicts.
- It can be used to merge different versions of the code.
- It also helps to make changes to and checkout remote branches while simultaneously avoiding conflicts.
- It helps to manage and handle releases without conflicts.
- Keeps the repository & process clean and readable.
Conclusion
Git has many uses and is extensively used by developers, product managers, and data scientists. Git commands are very effective and can be very useful. A conflict in git arises when two different branches try to edit the same line in a file, or a file gets deleted in a particular branch but edited in another. Git handles most of the merges successfully with its merging features, but it can also be resolved manually using tools like git status, git reset, git checkout, and Git log.
Recommended Articles
We hope that this EDUCBA information on “Resolve Merge Conflict in GIT” was beneficial to you. You can view EDUCBA’s recommended articles for more information.