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

后端 未结 4 1295
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 08:45

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

相关标签:
4条回答
  • 2020-12-12 09:04

    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.

    0 讨论(0)
  • 2020-12-12 09:05

    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
    
    0 讨论(0)
  • 2020-12-12 09:18

    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

    0 讨论(0)
  • 2020-12-12 09:20

    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.

    0 讨论(0)
提交回复
热议问题