我该如何仅从特定目录的“他们”分支接受git merge冲突?

问题描述

这是我的情况:git merge master导致50个文件发生合并冲突。我希望其中的45个文件可以从master中接受并完成,我想手动解决其余5个文件中的冲突。所有这些文件中的45个都位于目录{{1}中}。其他5个分散在其他地方。 如果,我只想接受所有50个冲突的管理员更改,就可以运行此命令:

some/dir

但是我不想要那样,就像我说的那样。我想要更像这样的东西:

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对所有文件中的所有冲突所做的更改。将我的一些手动修复的文件手动复制回仓库,然后提交。

这避免了手动修复50个文件的麻烦,当只有5个文件真正需要我注意时,这可能非常繁琐且耗时。

完整的详细步骤:

  1. 开始常规合并:

    master
  2. 仅手动解决我要在其中处理的5个文件中的冲突,然后保存每个文件然后,将它们的副本复制到回购协议之外的位置(或至少复制)在被仓库忽略的文件夹中)。我现在拥有手动解决的这5个文件的副本。这是完成此操作的一种方法

     git merge master
    
  3. 执行 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重做合并,除了这次自动接受所有50个文件的所有主更改(即:针对所有冲突)。

  4. 现在,从上方将我手动解析的备份副本手动复制回各自文件顶部的存储库中。

    git merge -X theirs master
  5. 最后,执行 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来将它们修改为合并提交,瞧!我有45个文件自动分辨率,和5个文件的手动分辨率。

有更好的方法吗?

我认为这种方法效果很好并且非常有效,但是我愿意学习替代方法,尤其是当它们更快或更容易时。

相关,但重复:

  1. Merging two branches,how do I accept one branch for all conflicts
  2. Simple tool to 'accept theirs' or 'accept mine' on a whole file using git

解决方法

解决冲突后,您可以直接从另一个分支检出文件。

因此,在完成git merge并解决您需要修复的文件的冲突之后。

执行git checkout <branch you're merging> -- some/dir这将仅通过那些更改将文件从另一个分支移出。我相信这也将使他们做好承诺。

如果ours分支中有更改,则可以仅在--之后列出文件,而不仅仅是检出整个目录。

,

在 6 个月前投票并标记正确的 this answer 之后,今天我意识到在一个非常大且非常拙劣的 merge 需要这个答案是错误的。假设您这样做是为了使用 feature_branch 的最新更改更新您的 master

git checkout feature_branch
git merge master
# conflicts result...

...而且有很多冲突。您会看到其中 25 个在 some/dir 内,并且您希望保留来自 feature_branch 的所有冲突更改,因此您执行以下 3 个命令中的任何一个(在本例中都是相同的):

git checkout -- some/dir
# OR
git checkout HEAD -- some/dir
# OR
git checkout feature_branch -- some/dir

好吧,你刚刚搞砸了! master 还在 some/dir 中添加了一些更改和新文件,其中许多与您的 feature_branch 更改零冲突,而所有这些都是您想要(这是将最新的 master 合并到您的 feature_branch 中的全部意义!)但现在它们全部消失,因为您只是用 {{1} 中的 some/dir 覆盖了它们}}。这根本不是我们想要的!

所以,这是正确答案:

feature_branch

再说一次,这与此不同:

# RIGHT ANSWER

# Keep only conflicting changes for all conflicts within files inside
# some/dir,from the "--ours" (`feature_branch` in this case) side.
git checkout --ours -- some/dir

所以,这是完整上下文中的正确答案:

# WRONG ANSWER

# Overwrite this entire directory 
git checkout feature_branch -- some/dir

完成!

有关详细信息和清晰度,请参阅我在此处的回答:Who is "us" and who is "them" according to Git?。这在那个答案的“警告警告”部分中有介绍,尽管这个答案是一个更全面的例子,更好地说明了问题。

,

我认为@Schleis的答案很好。如果您的回购壁球仍然合并,另一种选择是...甚至在开始合并之前,只需执行以下操作:

git checkout gabriel.staples/MyBranch
git checkout develop path/You/Want/To/Accept/Everything
git add path/You/Want/To/Accept/Everything
git commit -m "Partially merge develop at SHA XXXXX (squash-style)"
git merge develop
<resolve conflicts>
git add path/To/Your/Hand/Resolved/Files
git merge --continue

如果您确实需要,也可以使用以下方法使以上内容更接近真实的合并(将其设为父项):

git commit-tree aboveMergeSHA^{tree} -p `git merge-base develop pseudoMergeSHA` -p develop -m "Merge develop at SHA XXXXX"

但是到那时,我认为您最好也使用@Schleis的答案。我不喜欢在不需要时手动与父母打交道...