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.

Bite code
  • 578,959
  • 113
  • 301
  • 329

5 Answers5

381

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.

Waiting for Dev...
  • 12,629
  • 5
  • 47
  • 57
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

midor
  • 5,487
  • 2
  • 23
  • 52
keflavich
  • 18,278
  • 20
  • 86
  • 118
  • 7
    Love this one. Specially if there's more than one file. – Tek Aug 08 '14 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 '16 at 18:52
  • I went through at least five stackoverflow answers before finding this, which is what I wanted. Thanks. – Bolton Bailey May 18 '19 at 08: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. – soMuchToLearnAndShare Jul 27 '20 at 08:56
  • 1
    found the answer, sorry did not realize this thread was merge only, i thought it was merge conflicts . https://stackoverflow.com/a/4273436/4582240 – soMuchToLearnAndShare Jul 27 '20 at 09:02
  • error: Merging is not possible because you have unmerged files ... 8) – metamonkey Jun 07 '21 at 18:56
23

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.

Ben Wilde
  • 5,552
  • 2
  • 39
  • 36
  • 2
    No ambiguity, works for both {mine} and {theirs}, supports adding entire directories. **This should be the accepted answer.** – dotancohen Sep 14 '17 at 09: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
sehe
  • 374,641
  • 47
  • 450
  • 633
  • +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 '11 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 '11 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 '11 at 17:38
  • 1
    Added a solution based on git mergetool now – sehe Jul 11 '11 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.

Peter Kam
  • 95
  • 8
  • how is this different than the accepted answer? consider improving it to make relevant to the broader audience or removing it. – André Werlang Nov 08 '21 at 21:27