我如何接受来自“他们"的git merge冲突仅针对某个目录的分支?

问题描述:

这是我的情况:git merge master导致50个文件发生合并冲突.我希望其中的45个文件被master接受并完成,并且我想手动解决其余5个文件中的冲突.所有这些文件中的45个都位于目录some/dir中.其他5个散布在其他地方. 如果,我只想接受所有50个冲突的管理员更改,就可以运行此操作:

Here's my situation: git merge master results in 50 files with merge conflicts. I want 45 of them to just be accepted from master and be done, and I want to manually resolve conflicts in the remaining 5. All 45 of those files are in directory some/dir. The other 5 are scattered elsewhere. IF I wanted just to accept master's changes for all 50 conflicts, I'd run this:

git merge -X theirs master
# OR (same thing):
git merge --strategy-option theirs master

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

But I don't want that, like I said. I'd like something more like this:

git merge -X theirs master some/dir

,以便它自动选择他们的" (主)端处理some/dir中的所有冲突,但否则让我们手动解决冲突.但是,这不存在.

so that it automatically chooses "theirs" (master's) side for all conflicts in some/dir, but otherwise let's me manually fix the conflicts. This, however, doesn't exist.

所以,我的解决方法是:

简短说明:

开始合并,手动修复许多冲突文件中我需要的几个文件.备份我刚刚修复的所有文件.中止合并.重做与git merge -X theirs master的合并,以针对所有文件中的所有冲突自动接受master的更改.手动将我的一些手动修复的文件复制回仓库中,然后提交.

Start a merge, manually fix just the few files I need to, among the many many conflicting files. Back up any files I just fixed. Abort the merge. Redo the merge with git merge -X theirs master to automatically accept master's changes for ALL conflicts in ALL files. Manually copy my few manually-fixed files back into the repo, and commit them.

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

This avoids having to manually fix 50 files, which can be very tedious and time-consuming when only 5 really require my attention.

完整,详细的步骤:

  1. 开始常规合并:

  1. Start a normal merge:

 git merge master

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

  • Manually resolve conflicts in just the 5 files I want to do that in, and save each file. Then, MAKE COPIES OF THEM to a location outside of the repo (or at least in a folder that is ignored by the repo). I now have copies of those 5 files I manually resolved. Here is one way to accomplish this:

     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个文件的所有主更改(即:针对所有冲突).

  • Do git merge --abort to abort the merge, then do git merge -X theirs master to redo the merge, except this time automatically accepting all of master's changes for all 50 files (ie: for all conflicts).

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

    Now, manually copy my manually-resolved backup copies from above back into the repo on top of their respective files:

     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 -Agit commit --amend来将它们修改为合并提交,瞧!我有45个文件的自动分辨率,和5个文件的手动分辨率.

  • Finally, do git add -A and git commit --amend to amend them to the merge commit, and voila! I have the equivalent of an automatic resolution on the 45 files, and the manual resolution on the 5 files.

    有更好的方法吗?

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

    I think this way works pretty well and is pretty effective, but I'm open to learning alternatives, especially if they are faster or easier.

    1. 合并两个分支,如何为所有冲突接受一个分支
    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

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

    You can checkout the files from the other branch directly when resolving the conflict.

    因此,在执行git merge并解决您需要修复的文件的冲突之后.

    So after doing git merge and resolve the conflicts for the files that you need to fix.

    执行git checkout <branch you're merging> -- some/dir,这仅将那些更改从其他分支移出文件.我相信这也会使他们准备好提交.

    Do git checkout <branch you're merging> -- some/dir this will move the files over from the other branch with those changes only. I believe it will also make them ready to commit.

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

    If there are changes from the ours branch, you can just list the files after the -- rather than just checking out the whole dir.