DEV Community

Cover image for How Do I Resolve Merge Conflicts?

How Do I Resolve Merge Conflicts?

Rizèl Scarlett on March 18, 2022

I don’t think I’m alone in saying this; early in my career, merge conflicts were the bane of my existence (particularly in 2019). I graduated from ...
Collapse
 
tobisgd profile image
TobiSGD

I am glad that I can use a proper IDE, working with JetBrains-IDEs make this stuff so much easier, where you have full Git integration with a proper side-by-side diff-view and can apply or dismiss changes with a single click.

Collapse
 
mattiasnixell profile image
Mattias Nixell

Using Jetbrains products' git integration is like having a dishwasher instead of doing dishes by hand. You really can't live without it once you have it, because it makes life so easy! It's simply amazing!

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Oh wow! Okay, I'm sold!

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Interesting! I'll have to look into how JetBrains does it. I wouldn't describe VS code as hard. They have the option to see it side by side. You can apply and dismiss the changes in one click there as well, but if you're newer to version control ..the terminology of "current changes" or "incoming changes" can seem confusing.

Here's a post about the different ways to handle merge conflicts in different IDEs..might be interesting to compare those to JetBrains: betterprogramming.pub/how-to-resol...

Collapse
 
neatshell profile image
Claudio Stella

I think this is THE reason why I've never really switched to VSCode. And I cannot understand why is so difficult to provide a plugin that handles the conflicts like JetBrains-IDEs do.

Collapse
 
aquatofana profile image
Aqua Tofana

I mean, there's gitlens on VScode.. you can compare your code with the last commit and easily handle conflicts.

Collapse
 
valenciawhite profile image
Valencia White

My last group project in my coding bootcamp was a nightmare only because daily we were having merging conflicts, so it's safe to say the fear is now gone lol! But this article did provide some new insight to how to better manage and prevent these hiccups from happening in the first place! Not always avoidable, but definitely manageable. Thank you Rizèl ✨

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Lol, glad you got that experience in your group. While it was probably frustrating, now you're more prepared for future situations. Definitely manageable, and there's actually ways to avoid it or have less merge conflicts. Maybe I will write about that soon. And thank you for reading and commenting!

Collapse
 
valenciawhite profile image
Valencia White

Whaaatt??!! Then yes, yes, yes please write about it! And of course your content is engaging and informative, best of both worlds!

Collapse
 
sickpuppysoftware profile image
Matt King

Just be glad you didn't use Microsoft Visual SourceSafe. The merging was ok for the time but having to chase down someone on holiday to unlock a checked out file was a nightmare. Then the "database" would corrupt.

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

LOL I'm grateful I never experienced that

Collapse
 
star_trooper profile image
Atharva Shirdhankar

Awesome and helpful writeup😃💯

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

I'm glad it was! Let me know if you have any other questions.

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Update: here's my thoughts on preventing merge conflicts -

dev.to/github/how-to-prevent-merge...

Collapse
 
netch80 profile image
Valentin Nechayev

A few principal notes.

​1. You (among with most of such recipesʼ authors) have omitted one principal thing: in a complex case, to understand how a conflict should be resolved, you might see an original base of each conflicting chunk. To make it visible in conflicts, use setting: git config merge.conflictStyle diff3 (you may add --global for global setting).

This will add a new section, like in the following example:

original:

1
2
3
Enter fullscreen mode Exit fullscreen mode

version1:

one
2
3
Enter fullscreen mode Exit fullscreen mode

version2:

1
two
3
Enter fullscreen mode Exit fullscreen mode

After merge attempt:

<<<<<<< HEAD
one
2
||||||| b58938fa
1
2
=======
1
two
>>>>>>> two
Enter fullscreen mode Exit fullscreen mode

You see new section after |||||||| - this is the common base.
Not only a human gets aid from it - this is crucial for automated merge tools.

​2. Next: use automated merge tools. There are lots of them: kdiff3, vimdiff... They have two merits: 1) they are deliberately smarter than default git logic and may resolve conflicts that git retains unresolved; 2) they provide visual interface to merge.
For a command in git, this is git mergetool (shall be configured, what tool to run).

​3. Use aid to understand what change is applied now to make the conflict. Depending on operation, this could be git status plus git show for specified commit id (shows the applied patch - typical for rebase), git am --show-current-patch (I use alias git amsh for it; used for cherry-pick as well).

​4. Notice that sometimes git misdetects what chunks to compare. You might need editor to relocate code blocks to realize the proper relation between chunks. I usually experienced this in case of sequence of small methods when something is inserted inside the sequence.

Collapse
 
worc profile image
worc

+1 to calling in the other author you're conflicting with. on the command line a merge conflict looks and is arcane, cryptic, and math-heavy. but the reality is that it's just you and another person collaborating asynchronously.

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

You worded that well!

Collapse
 
blindfish3 profile image
Ben Calder

Good article! It might be worth mentioning that as well as accepting current and/or incoming changes you can just grab code from each change and manually edit what you want to keep/remove (at which point manually removing the change indicators is also required).
Obviously the best strategy is to avoid merge conflicts in the first place. That's achieved by careful planning; good communication between team members; and can also be mitigated by keeping up with the base branch (i.e. rebasing regularly).

Collapse
 
hayleyguez profile image
HayleyGuez

How to handle merge conflicts in Pulling remote changes to a local repository ? powerful dua for marriage with a loved one

Collapse
 
roneo profile image
Roneo.org

I switched from Vscode to Meld to solve conflicts, give it a try: it's very handy and visual

Collapse
 
slopez93 profile image
Santi López Bayo

You can comment here also:

eccco.space/s/cadf73c1-1900-4fe8-a...

Collapse
 
josetonyin profile image
josetonyin

I have used P4Merge tool to resolve conflicts. Give it a try.
perforce.com/products/helix-core-a...

Collapse
 
valdineisantos profile image
Valdinei dos Santos

I hate git conflict , 1 hour to add feauture in your project and 7 hour to resolve conflicts

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

Hopefully, this post helps to reduce that time. I'll write a post about how to prevent merge conflicts in the future.