255

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.

5 Answers 5

382

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.

15
  • 5
    I did this.. but nothing happens.. how do I know it is taking the correct file? I am using git version 1.8.4 if that matters. Mar 14, 2014 at 5:24
  • 3
    why the inverse word use? I assume "theirs" would be remote file and "ours" would be my file
    – phuclv
    Mar 11, 2017 at 10:11
  • 1
    So it is, @LưuVĩnhPhúc Mar 12, 2017 at 14:12
  • 8
    no, their meanings are reversed stackoverflow.com/q/2959443/995714 stackoverflow.com/q/29324812/995714 theirs would be my files and ours are the files in the remote branch
    – phuclv
    Mar 12, 2017 at 14:55
  • 7
    "Easy", yes. Intuitive? No. Aug 8, 2017 at 0:26
145

This approach seems more straightforward, avoiding the need to individually select each file:

# keep remote files
git merge --strategy-option theirs
# keep local files
git merge --strategy-option ours

or

# keep remote files
git pull -Xtheirs
# keep local files
git pull -Xours

Copied directly from: Resolve Git merge conflicts in favor of their changes during a pull

6
  • 7
    Love this one. Specially if there's more than one file.
    – Tek
    Aug 8, 2014 at 17:34
  • The question was for two different commands, but there's no description as to what these two do. What does each line do?
    – Alex
    Oct 18, 2016 at 18:52
  • I went through at least five stackoverflow answers before finding this, which is what I wanted. Thanks. May 18, 2019 at 8:13
  • Hi @keflavich, if i am doing rebase from 1 branch to another, and l'd like to use this git merge --strategy-option theirs strategy, at which point should i put this command in? and is it temporary only? I'd like it to only keep effect in this rebase, not the future one. Jul 27, 2020 at 8:56
  • 1
    found the answer, sorry did not realize this thread was merge only, i thought it was merge conflicts . stackoverflow.com/a/4273436/4582240 Jul 27, 2020 at 9:02
24

git checkout {branch-name} -- {file-name}

This will use the file from the branch of choice.

I like this because posh-git autocomplete works great with this. It also removes any ambiguity as to which branch is remote and which is local. And --theirs didn't work for me anyways.

1
  • 2
    No ambiguity, works for both {mine} and {theirs}, supports adding entire directories. This should be the accepted answer.
    – dotancohen
    Sep 14, 2017 at 9:09
11

For the line-end thingie, refer to man git-merge:

--ignore-space-change 
--ignore-all-space 
--ignore-space-at-eol

Be sure to add autocrlf = false and/or safecrlf = false to the windows clone (.git/config)

Using git mergetool

If you configure a mergetool like this:

git config mergetool.cp.cmd '/bin/cp -v "$REMOTE" "$MERGED"'
git config mergetool.cp.trustExitCode true

Then a simple

git mergetool --tool=cp
git mergetool --tool=cp -- paths/to/files.txt
git mergetool --tool=cp -y -- paths/to/files.txt # without prompting

Will do the job

Using simple git commands

In other cases, I assume

git checkout HEAD -- path/to/myfile.txt

should do the trick

Edit to do the reverse (because you screwed up):

git checkout remote/branch_to_merge -- path/to/myfile.txt
4
  • +1 for the tips, but not accepted butcause it's not what I asked for. I want something that work in all the cases, not just in the example cases. Plus "git checkout remote/branch_to_merge -- path/to/myfile.txt" won't work if you started your merge already: it will say that you are in the middle of a merge and will prevent you from doing so.
    – Bite code
    Jul 11, 2011 at 16:56
  • 1
    @e-satis: that's surprising. I would consider that a bug, since checkout with a path is not a regular checkout (and it doesn't affect HEAD). I'm gonna try it out now because I can't believe it
    – sehe
    Jul 11, 2011 at 17:35
  • 1
    @e-satis: Soooo... I was right; git checkout remote/branch_to_merge -- path/to/myfile.txt works like a charm while resolving a merge conflict. (git 1.7.1)
    – sehe
    Jul 11, 2011 at 17:38
  • 1
    Added a solution based on git mergetool now
    – sehe
    Jul 11, 2011 at 17:52
0

I asked the questions a day ago, very similar this quesiton but my questions is slightly different because it is something to do with a database, sqlite3.

but a manager on this site asked me to delete the quesition so I did delete it but he finally blocked me.

Anyway, the answer to my matter was "git checkout --their db.sqlite3"

Something related with databse, then just simply "git checkout --their db.sqlite3" not need to put path.

I leave this answer for someone who got same problem as me.

1
  • how is this different than the accepted answer? consider improving it to make relevant to the broader audience or removing it. Nov 8, 2021 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.