Introduction to Resolve Merge Conflict in GIT
Managing contributions to resolve conflicts among multiple distributed developers is one of the main jobs of the version control systems. 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 it and how it 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, it 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 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 but if unable to do so it’ll leave it up to you to manually resolve it.
How to a Create File Merge Conflict?
Let’s take an example to show/resolve merge conflict in GIT using the command line Git interface.
$ 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.
$ 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 commits the newly created content.
With the newbranch named one branch, a commit is created that overwrites the content in new_merged.txt
$ 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!
$ git merge newbranch
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
git commit -m "merged and resolved the conflict in
Git ensures resolving of the conflict and thus creates a new committed merge to finalize it. Some of the Git commands which help to resolve merge conflicts are:
git log --merge
The merge –abort passed with git log command produces a list of commits conflicting between merging branches.
It helps to identify conflicted files.
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.
git reset --mixed
It is used to undo the changes made to the current directory.
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.
It is used to reset conflicted files during a merge conflict.
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
- 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.
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.
This is a guide to Resolve Merge Conflict in GIT. Here we discuss how to efficiently resolve Git Merge Conflicts with various commands along with its advantages. You can also go through our other suggested articles to learn more –
- Git Alternatives with Advantages
- Introduction to Git Tools
- GitHub vs SVN – Top Differences
- Top 11 GIT Interview Questions
The post Resolve Merge Conflict in GIT appeared first on EDUCBA.