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

GitMergeLocal

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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: https://stackoverflow.com/questions/10697463/resolve-git-merge-conflict-accepting-their-changes

Solution 3 - Git

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.

Solution 4 - Git

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

Solution 5 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questione-satisView Question on Stackoverflow
Solution 1 - GitWaiting for Dev...View Answer on Stackoverflow
Solution 2 - GitkeflavichView Answer on Stackoverflow
Solution 3 - GitBen WildeView Answer on Stackoverflow
Solution 4 - GitseheView Answer on Stackoverflow
Solution 5 - GitPeter KamView Answer on Stackoverflow