DEV Community

Cover image for git merge ours
Waylon Walker
Waylon Walker

Posted on • Edited on • Originally published at waylonwalker.com

git merge ours

Sometimes you have a pretty old branch you are trying to merge into and you are absolutely sure what you have is what you want, and therefore you don't want to deal with any sort of merge conflicts, you would rather just tell git to use my version and move on.

update main

The first step is to make sure your local copy of the branch you are moving into is up to date.

git checkout main git pull
Enter fullscreen mode Exit fullscreen mode

update your feature branch

It's also worth updating your feature branch before doing the merge. Maybe you have teammates that have updated the repo, or you popped in a quick change from the web ui. It's simple and worth checking.

git checkout my-feature git pull
Enter fullscreen mode Exit fullscreen mode

start the merge

Merge the changes from main into my-feature branch.

git merge main
Enter fullscreen mode Exit fullscreen mode

Now is where the merge conflict may have started. If you are completely sure that your copy is correct you can --ours, if you are completely sure that
main is correct, you can --theirs.

git checkout --ours .
git merge --continue
Enter fullscreen mode Exit fullscreen mode

This will pop open your configured git.core.editor or $EDTIOR. If you have not configured your editor, it will default to vim. Close vim with <escape>:x, accepting the merge message.

Now push your changes that do not clash with main and finish your pr.

git push
Enter fullscreen mode Exit fullscreen mode

If you liked this one checkout git-find-deleted-files to find all the files you have deleted, but still exist somewhere in git.

Discuss

Do you use --our/--theirs? With merge or maybe even rebase?

Top comments (3)

Collapse
 
moopet profile image
Ben Sinclair

I've never used this. I remember (I think?) reading about it a long time ago but it's just one of those things I've never done. Like how using bisect is a great idea but unless you have a quick way of testing whether the branch is good or bad it can take longer than eyeballing the code at random commits.

BTW, I think you missed a newline in your git checkout --ours . git merge --continue - like I say, I've not used it but I'm assuming you can't chain commands on one line like that.

Collapse
 
waylonwalker profile image
Waylon Walker

It's one of those things that is super handy when you need it.

Collapse
 
waylonwalker profile image
Waylon Walker

thanks for highlighting that. I write with hardwraps and have a tool to unrap as dev does not like hardwraps, and sometimes a codeblock sneaks in and gets wrapped.