How to keep the local file or the remote file during merge using Git and the command line?

e-satis picture e-satis · Jul 11, 2011 · Viewed 122k times · Source

I know how to merge modification using vimdiff, but, assuming I just know that the entire file is good to keep or to throw away, how do I do that?

I don't want to open vimdiff for each of them, I change want a command that says 'keep local' or 'keep remote'.

E.G: I got a merge with files marked as changed because somebody opened it under windows, changing the EOL, and then commited. When merging, I want to just keep my own version and discard his.

I'm also interested in the contrary: I screwed up big time and want to accept the remote file, discarding my changes.

Answer

Waiting for Dev... picture Waiting for Dev... · Aug 19, 2012

You can as well do:

git checkout --theirs /path/to/file

to keep the remote file, and:

git checkout --ours /path/to/file

to keep local file.

Then git add them and everything is done.

Edition: Keep in mind that this is for a merge scenario. During a rebase --theirs refers to the branch where you've been working.