How to automatically resolve a Git conflict by taking the version in the current branch?
Solution 1
If you want to do it as a one-off, the single-line command is:
$ git checkout --ours foo/bar.txt # <-- resets it only if the merge found conflicts in this file
$ git checkout HEAD -- foo/bar.txt # <-- resets it to that specific version no matter what
To configure git's merge to permanently ignore all upstream changes to a locally-changed file:
$ git config merge.pin.driver true
$ echo foo/bar.txt merge=pin >> .git/info/attributes
(true
above is just the unix true
command, its success says it made the local version look right, in this case by doing nothing to it. You can of course get more sophisticated with your merge commands.)
I think you don't want merge --strategy=ours
or --strategy-option=ours
, those apply to entire merges.
Solution 2
You can specify the merge strategy option ours
to the recursive
(the default) merge strategy. It will do a normal merge, but in case of conflicting hunks will choose the version of the current branch.
git checkout A
git merge -Xours B
pts
Programming for fun and profit. Hacking together TeX macros. Written gzip decompressor in PostScript. Writing multilingual programs, such as: %:/*:if 0;"true" +s ||true<</;#|+q|*/include<stdio.h>/*\_/ {\if(%)}newpath/Times-Roman findfont 20 scalefont setfont( %%)pop 72 72 moveto(Just another PostScript hacker,)show(( t)}. t:-write('Just another Prolog hacker,'),nl,halt. :-t. :-initialization(t). end_of_file. %)pop pop showpage(-: */ int main(){return 0&printf("Just another C%s hacker,\n",1% sizeof'2'*2+"++");}/*\fi}\csname @gobble\endcsname{\egroup \let\LaTeX\TeX\ifx}\if00\documentclass{article}\begin{doc% ument}\fi Just another \LaTeX\ hacker,\end{document}|if 0; /(J.*)\$sh(.*)"/,print"$1Perl$2$/"if$_.=q # hack the lang! / sh=sh;test $BASH_VERSION &&sh=bash;test $POSIXLY_CORRECT&& sh=sh;test $ZSH_VERSION && sh=zsh;awk 'BEGIN{x="%c[A%c[K" printf(x,27,27)}';echo "Just another $sh hacker," #)pop%*/
Updated on January 07, 2020Comments
-
pts over 4 years
Let's suppose that I get a merge conflict on
foo/bar.txt
when running this:$ git checkout A $ git merge B
I'd like to automatically resolve the conflict by taking
foo/bar.txt
from branch A. (I know what I'm doing, and I do need this. The version in branch B is wrong, and I don't care about losing changes in the working tree in this case.) It seems that I can do it by running these commands:$ git reset foo/bar.txt $ git checkout foo/bar.txt
Is there a simpler, single-command solution?
Unfortunately these commands change
foo/bar.txt
even if there is no conflict, and I don't want that. If there is no conflict, I want want to keepfoo/bar.txt
in whatever stategit merge B
has left it.So I need a Unix shell command, which would detect if there is a conflict in
foo/bar.txt
, and if there is, it would resolve the conflict by taking the version offoo/bar.txt
from the current branch. It wouldn't do anything else, i.e. it wouldn't modify other files, it wouldn't commit the changes, and it wouldn't changefoo/bar.txt
if there is no conflict in that file. -
pts over 11 yearsThank you for this additional information. Unfortunately it doesn't answer my question, because the
git merge B
command (and possibly many other uncommitted filesystem changes) has already been run. -
knittl over 11 yearsIf you have already run the merge command, then you must use the
reset && checkout
method. -
pts over 11 yearsThe reset+checkout method is not an answer to my question either, because it's not a no-op if there is no conflict (see the question for details).
-
knittl over 11 years@pts: yeah, but I didn't say you have to do it unconditionally. And it's not a no-op, because there might be unconflicting changes which get merged just fine by Git's automatic conflict resolution.