无需手动编辑每个文件即可解决合并冲突

resolve merge conflicts without manually editing each file

我是 Git 的新手,我正在尝试找到一种更快的方法来解决合并冲突。假设我试图将一个分支合并到 master 并且我遇到了一些文件的合并冲突。

Changes to be committed:

    modified:   ...

Unmerged paths:
   (use "git add <file>..." to mark resolution)

    both modified:   ...

Git 进入每个修改后的文件并添加冲突解决标记,对吗? (<<<<<<< 这个东西)。通常我会进入文件并手动更改它们,但在这种情况下它们都是 bin 文件并且与我的程序工作不太相关。有没有一种方法可以暂存所有 modified 文件并指定我希望所有 unmodified 文件保持合并前 master 中的状态。 (基本上忽略从分支到那些文件的更改)。谢谢

合并时,可以指定一个"strategy"。来自 git help merge:

ours

This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.

您必须找到要使用的文件副本,将其签出,暂存更改,然后提交。例如:

git log 

(find out we want the file from commit ac3422d4ceba793ff9fd3df81159d23111a760e2)

git checkout ac3422d4ceba793ff9fd3df81159d23111a760e2 file/to/fix.txt
git add file/to/fix.txt
git commit -m "success!!"

它的作用是:

  • 用指定提交中的文件替换文件
  • 暂存更改并且将文件上的合并冲突标记为已解决
  • 提交 change/merge