1

In Visual Studio, I can easily take the current (or incoming) branch's changes while resolving merge conflicts. This is like merging with --ours (or --theirs) merge strategy for a single file. Could someone please tell me how I would do this on the command line :)

I have tried git checkout --ours -- $filename. That exits with code 0, but when I run git status, the file still shows as "both added". I am baffled at why this does not work.

Peter Bergman
  • 472
  • 4
  • 16

2 Answers2

1

You can also try in one command git restore --ours, which replaces git checkout (since Git 2.23 Q3 2019):

git restore -SW --ours -- $filename

That would restore and add the file to the index.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

It turns out that I had to run git checkout, as done in the question:

git checkout --ours -- $filename.

THEN, I had to run git add to get rid of the "both added" marking and finish resolving the conflicts in that file:

git add $filename

I can put them together like so:

git checkout --ours -- $filename && git add $filename

Peter Bergman
  • 472
  • 4
  • 16
  • 1
    You can add the file after you have taken care of all modifications required on the file. No need to do it right after checkout to then continue editing the file. – eftshift0 Dec 05 '22 at 17:59