Merge Conflicts, What , How , Why

What are Merge Conflicts

Conflicts while merging are a frequent part of the Git experience. If several developers are working on the same file the odds of encountering a merge conflict increase. Most of the time, Git will automatically figure out how to integrate new changes. Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it and GitHub shows you that you're some commits behind from the base branch or repo. In these cases, Git cannot automatically figure out which is correct. Hence, Git will notify the developer performing the merge that a conflict is encountered, and the rest of the team will be unaware of the conflict. Now, it is the responsibility of the developer performing the merge, to resolve the conflict and be able to merge with the head branch to have his/her contribution to the project.

Types Of Merge Conflicts

While starting the merge: If there are changes in either the working directory or staging area, while merging, then Git will fail to start the merge. This happens because the pending changes could be overridden by the commits that are being merged. This is the error message provided by Git when this type of merge conflict happens :

error: Entry '<fileName>' not uptodate. Cannot merge. (Changes in working directory)
or,
error: Entry '<fileName>' would be overwritten by merge. Cannot merge. (Changes in staging area)

This type of conflict can be resolved either by doing git stash save “any_message_to_describe_what_is_saved” (Stashes away any changes in your staging area and working directory in a separate index) OR git checkout <file_name> (throws out your changes), and then the merge can be completed.

During the merge: This occurs because you have committed changes that conflict with someone else’s committed changes. Git will do its best to merge the files and will leave things for you to resolve manually in the files it lists. This is the error message provided by Git when this type of merge conflict happens :

CONFLICT (content): Merge conflict in <fileName>
Automatic merge failed; fix conflicts and then commit the result.

How to Resolve Merge Conflicts

The first step in resolving merge conflicts is to understand what is going on, from the header above we can see an easy way to resolve merge conflicts when you're just starting the merge but in some cases, if its a personal project and there are merge conflicts, that is, your not working with a base branch or head branch in some other repo, what you can do is to create a pull request from your local environment where Git runs and then allow changes that may be conflicting with your remote repository be pulled into your local repository on your machine.

Furthermore, If you're facing a different kind of merge conflict where there is a head branch, then the simplest solution is to delete your fork of the repo in your account on GitHub, fork the repo again, create a clone of that branch or the whole repo and then since it's a fresh clone then it's not going to have any conflicts with the base repo, make sure that the project which contains the changes you want to make still exists locally on your system, after doing this you can take the changes which exist on your local system, update the recent clone with those changes, and then push to your remote repo, that is your fork of the project, try making a pull request, you'd see that you'd be some commits ahead of the parent branch and no commits behind, Congratulations you've resolved your merge conflict!!

Thanks for Reading!!!, Do like, share and subscribe