【发布时间】:2020-12-16 18:51:11
【问题描述】:
这是我的情况:git merge master 导致 50 个文件存在合并冲突。我希望其中的 45 个从 master 被接受并完成,并且我想手动解决剩余 5 个中的冲突。所有 45 个文件都在目录 some/dir 中。其他 5 个分散在其他地方。 如果我只想接受 master 对所有 50 个冲突的更改,我会运行这个:
git merge -X theirs master
# OR (same thing):
git merge --strategy-option theirs master
但我不想那样,就像我说的那样。我想要更像这样的东西:
git merge -X theirs master some/dir
以便它自动为some/dir 中的所有冲突选择“他们的”(主人)一方,否则让我手动修复冲突。然而,这并不存在。
所以,我的解决方法是:
简短说明:
开始合并,在众多冲突文件中手动修复我需要的几个文件。备份我刚刚修复的所有文件。中止合并。重做与git merge -X theirs master 的合并以自动接受master 对所有文件中所有冲突的更改。手动将我的几个手动修复的文件复制回 repo,并提交它们。
这避免了必须手动修复 50 个文件,当我真正需要注意的只有 5 个文件时,这可能会非常乏味和耗时。
完整的详细步骤:
-
开始正常的合并:
git merge master -
手动解决我想要在其中执行此操作的 5 个文件中的冲突,并保存每个文件。 然后,将它们复制到 repo 之外的位置(或至少在文件夹中回购忽略了这一点)。我现在有我手动解决的那 5 个文件的副本。这是实现此目的的一种方法:
mkdir -p temp # create `temp` dir inside the repo. # Note: if you're not aware, the ".git/info/exclude" file in your repo is # an **untracked** form of a ".gitignore" file for the repo. We will use # this file below. # Add a `/temp/` entry to the ".git/info/exclude" file to keep # the repo from tracking the /temp/ dir, but withOUT using the # shared ".gitignore" file since this is my own personal setting # not everyone else on the team necessarily wants. echo -e "\n# don't track this temporary folder for my arbitrary use\n/temp/" \ >> .git/info/exclude # Now manually make copies of your 5 files you just resolved conflicts in: cp some/dir/file1.cpp temp cp some/dir/file2.cpp temp cp some/dir/file3.cpp temp cp some/dir/file4.cpp temp cp some/dir/file5.cpp temp -
执行
git merge --abort中止合并,然后执行git merge -X theirs master重新执行合并,但这次自动接受所有50 个文件的所有master 更改(即:对于所有冲突)。 -
现在,手动将我手动解析的备份副本从上面复制回各自文件顶部的存储库中:
cp temp/file1.cpp some/dir cp temp/file2.cpp some/dir cp temp/file3.cpp some/dir cp temp/file4.cpp some/dir cp temp/file5.cpp some/dir -
最后,执行
git add -A和git commit --amend将它们修改为合并提交,瞧!我对 45 个文件进行了自动解析,对 5 个文件进行了手动解析。
有没有更好的方法?
我认为这种方法效果很好,也很有效,但我愿意学习替代方法,尤其是如果它们更快或更容易的话。
相关,但不重复:
【问题讨论】:
-
我想你可能正在寻找this answer,从例如获取剩余的未合并文件。
git ls-files -u,在每个节点上运行脚本解析器,添加结果。