Resolving git merge conflicts

When two developers develop two different branches of code, and touch the same files (binary conflict) or lines (version control tools can use command line command diff to find out the changes of the lines within a text file), the file is in conflict. On GitLab the error message is:

"You can merge this merge request manually using the command line"

The conflict markers (>>>>>, ====, <<<<) show you the place of the conflict.

What you should next do as a developer is to resolve the conflict by editing the conflict markers away and restoring the file in a working order by

* Choosing the upper part

* Choosing the lower part

  • * Editing the file and merging the changes
  •  

Hint! Ask the author of the other commit for help.

Advanced hint! Enable git rerere to automatically use previous merge conflict resolutions when they occur again.

Note! Never commit files that have conflict markers in them! There are multiple conflict markers in the file. Always self-review the changes before committing (git diff HEAD^) .

Note! The conflict markers are used also by other version control tools such as SVN.

Git conflict resolution by Command Line + Nano

There are many ways how you can resolve merge conflicts, but here is the process that is the easiest and fastest in my opinion also for new developers. 

The only requirement is that you know how to use command line tools, but Nano is the easiest editor available for beginners (of course you can use vim if you already know how to quit it :))

git checkout develop
git pull origin develop
git checkout -
git merge develop
git status

Now you see red files that are in conflict. For each file repeat the following:

nano

Next search for the conflict marker by ctrl-w ==== 

Note! Please search for 4 times equals mark since in JS, PHP and other languages === is strict comparison evaluation.

Use ctrl-k to cut the conflict marker line, and upper or lower part so that the conflict is resolved. Here you need some knowledge of the code, which part would be the right one, or the combination of both, edited manually.

Note! Please avoid adding new code or features within merge conflicts. You can do that in practice, but it makes tracking the changes very difficult if you introduce new stuff in a commit that claims to be a conflict resultion. Do a separate commit instead.

Important! Search again for the middle confict marker by pressing ctrl-w. Repeat until you can’t find any more markers.

 Save (ctrl-x, y(es) and enter in nano)

 git add filename
 git status

Repeat for the next file.

Finally git commit.

You are done and can next maybe git push.

Your changes to the origin server!

If you are not familiar with the command line (which are the most powerful tools that a developer can hope for and you should invest time heavily to learn them to increase your productivity by switching over to Linux), you can also use more visual tools such as

* Meld
* Jetbrains IntellJ Idea
* Visual Studio Code