如何对多个文件(或所有未合并的文件)执行“git checkout --theirs”操作

52

假设我在尝试合并后,在输入 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

...而且我知道我想要对这些文件中的每一个执行此操作:

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

... 但它们有很多。我该如何批量处理它们?

4个回答

62

对于我的情况,解决方案是在目录路径中使用通配符,因为文件是分组的:

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

根据helios456的说法,这也可能有效:

git checkout --theirs directory_name/.

7
您可以使用以下命令来检出未合并路径上的多个文件。
git checkout --theirs `git status | grep "added by them:" | awk '{print $NF}'`

执行以下命令来提交上述文件。
git commit `git status | grep "added by them:" | awk '{print $NF}'`

4

如果你的文件存在多层目录(即有子目录),而你想在一个目录下递归选择所有它们的文件,那么请使用以下方法:

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

(from this page)


如果存在二进制文件,则此方法无法正常工作。 - C.J. Jackson

2

这个命令会检查所有未合并的文件,而无需指定目录:

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

Source


1
这个答案如果能解释一下所有的参数和标志会更好。 - isherwood

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接