DEV Community

Cover image for How to git resolve conflicts quickly from two commands lines.
darker
darker

Posted on • Edited on

How to git resolve conflicts quickly from two commands lines.

When rebasing/merging, you may face sometimes conflicts, and if you're like me... "LAZY", you just want to get all new changes from the main branch or keep all your current changes as fast as possible depending on the file !

Example

So, for conflicts on some file, you may need to get incoming changes or keep your current changes ! I made two alias for that, git add-their and git add-our !

Under the hood, what it's done is quite simple, in your ~/.gitconfig section, you just have to add those lines :

[alias]
      add-their = !git checkout --ours $@ && git add $@
      add-our = !git checkout --theirs $@ && git add $@
Enter fullscreen mode Exit fullscreen mode

So, how this work, it's mostly two main commands, a checkout command and an add command, let's focus on keywords (theirs and ours) and how they are reverted !

In a context of rebasing, if you're willing to set current changes with these aliases, you should do git add-our and git add-their for incoming ones, that logic is inverted when you're dealing with merge conflicts. this is a good article that go deeply on explanations !

IMPORTANT NOTE : these aliases are only for the one knowing what they are doing, cuz on a complicated conflicts, there is some stuff you will want to keep on both side !

Top comments (4)

Collapse
 
fjones profile image
FJones

Please, don't. Take the time to understand how merges work, and what the files should look like after a merge. It's really not that hard.

This kind of blanket approach basically only works if the conflicts came from someone not using git correctly in the first place - otherwise you're tossing out changes you probably want to keep.

Collapse
 
sanixdarker profile image
darker

true, thanks for the comment, i added a side note !

Collapse
 
ccoveille profile image
Christophe Colombier

It sounds dangerous but thanks for sharing.

Collapse
 
sanixdarker profile image
darker

yep, it's dangerous !
i added a side note at the end !