56

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?

0

5 Answers 5

65

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/.
0
7

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}'`
0
4

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 an article on handling merge conflicts.)

1
  • This doesn't work if there are binary files present, though. Commented Oct 4, 2017 at 21:56
2

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

1
  • 1
    This answer would be better with some explanation of all those arguments and flags.
    – isherwood
    Commented Jul 29, 2016 at 14:00
-1

Use Folder/. instead

This simple solution worked for me!

So basically check what is the root folder for your project, in case it is called src, you can add them all like this:

git checkout --theirs src/.
1
  • How does this add to the accepted answer? It's also a repeat of an answer you deleted. Why wouldn't you simply undelete that one?
    – isherwood
    Commented Jan 31 at 14:04

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