git merge master
导致出现50个带有合并冲突的文件。我想接受其中45个来自master
的文件,并完成合并,我想要手动解决剩余的5个文件中的冲突。这45个文件都在some/dir
目录中。而其他5个文件则散布在其他地方。如果我只想接受所有50个冲突的文件中来自master的更改,我会执行以下命令:git merge -X theirs master
# OR (same thing):
git merge --strategy-option theirs master
但是我不想要那样,正如我所说的。我希望得到更像这样的东西:
git merge -X theirs master some/dir
使 Git 在 some/dir
中的所有冲突情况下自动选择“theirs”(主分支)方案,同时允许手动修复冲突。然而,这个功能并不存在。
因此,我的解决方法如下:
简要说明:
开始合并,仅手动修复我需要的少量文件,而不是所有冲突的文件。备份我刚刚修复过的任何文件。中止合并。使用 git merge -X theirs master
重新执行合并以自动接受所有文件中的所有冲突的 master
更改。手动将我修复的几个文件复制回代码库,并提交它们。
这避免了手动修复50个文件,当只有5个文件确实需要注意时,这可能非常乏味和耗时。
详细步骤:
Start a normal merge:
git merge master
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
Do
git merge --abort
to abort the merge, then dogit 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
Finally, do
git add -A
andgit 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.
有更好的方法吗?
我认为这种方式相当有效,并且效果很好,但我愿意学习其他替代方案,特别是如果它们更快或更容易。
更新:这里是答案。 目前最受欢迎的答案实际上是不正确的,在某些非常重要的情况下会产生错误的行为,尽管它是最受欢迎的。
git ls-files -u
获取剩余的未合并文件,在每个文件上就地运行脚本解析器,然后添加结果。 - jthill