How to do 'git checkout --theirs' for multiple files (or all unmerged files)

40,653

Solution 1

The solution for my case ended up being to simply use a wildcard in the directory path, since the files were grouped:

git checkout --theirs directory_name/*
git add directory_name/*

This may also work, according to helios456:

git checkout --theirs directory_name/.

Solution 2

You can use the below commands to checkout multiples files on unmerged path

git checkout --theirs `git status | grep "added by them:" | awk '{print $NF}'`

execute the below command to commit the above files

git commit `git status | grep "added by them:" | awk '{print $NF}'`

Solution 3

If your files are deeper than one directory (ie. there are subdirectories) and you want to selectively recursively choose all their files for a directory, then use the following:

grep -lr '<<<<<<<' directory_name/ | xargs git checkout --theirs
git add directory_name/*

(from this page)

Solution 4

This checks out all unmerged files without specifying the directory:

git ls-files --unmerged | perl -n -e'/\t(.*)/ && print "$1\n"' | uniq | xargs -r git checkout --theirs --

Source

Share:
40,653
isherwood
Author by

isherwood

I stand with the people of Ukraine. Putin's senseless murder and destruction must not be tolerated. Web software developer with a UX specialty. Motorcyclist, photographer, daddy. Budding bonsai enthusiast. Fourth-generation carpenter and former home builder. Perpetual home-improver. Once and again private pilot. I do a lot of Q&amp;A editing on Stack Exchange. Partly I want to make the information here as useful, clear, and easy to read as possible. Partly I'm satiating my mildly obsessive-compulsive personality. I hope that's ok. P.S. My handle isn't @isherwood. Just isherwood, thank you very much (after the protagonist in my favorite novel). @ is an operator that directs my attention to your mention of me. Don't use it unless that's your intent. The more you know! =====★ P.P.S. Isherwood thinks that profile blurbs written in the third person are creepy. He wonders who you commissioned to write it for you (and, apparently, place it in your profile on your behalf). P.P.P.S. "im" isn't a word in English. If you want to be taken seriously as a developer, put some effort into your written communication. Find your shift key and the apostrophe and use them to properly spell the contraction of "I am". It's not hard.

Updated on July 09, 2022

Comments

  • isherwood
    isherwood almost 2 years

    Say I have this after attempting a merge and upon entering git status:

    # Unmerged paths:
    #   (use "git add/rm <file>..." as appropriate to mark resolution)
    #
    #   added by them: myfile1.xhtml
    #   added by them: myfile2.xhtml
    #   added by them: myfile3.xhtml
    

    ... and I know I want to do this for each of those files:

    git checkout --theirs myfile1.xhtml
    git add myfile1.xhtml
    

    ... but there are many of them. How might I do them as a batch?