如何在Git中重新合并文件?

43

我有10个文件在合并分支时冲突。我已经解决了这10个文件的所有冲突(花费了很长时间)。不幸的是,在提交之前,我发现一个文件被错误地合并了,需要重新开始处理该文件。 :(

在Git中,如何标记已合并的文件未合并,换句话说,如何重新合并该文件?


1
可能是仅重新合并单个文件的重复问题。 - Milad Khajavi
1个回答

60
git checkout -m <filename>
这将从索引中删除它,并恢复为一个“冲突”的文件,该文件具有执行合并所需的所有标记。
来自git help checkout man页面:
-m, --merge
    When switching branches, if you have local modifications to
    one or more files that are different between the current
    branch and the branch to which you are switching, the command
    refuses to switch branches in order to preserve your
    modifications in context. However, with this option, a
    three-way merge between the current branch, your working tree
    contents, and the new branch is done, and you will be on the
    new branch.

    When a merge conflict happens, the index entries for
    conflicting paths are left unmerged, and you need to resolve
    the conflicts and mark the resolved paths with git add (or git
    rm if the merge should result in deletion of the path).

    When checking out paths from the index, this option lets you
    recreate the conflicted merge in the specified paths.

(最后一句话是最重要的)

这里有一篇博客文章,描述了为什么添加了这个功能以及在较旧版本的Git中如何不可能实现:http://gitster.livejournal.com/43665.html


1
+1 太棒了!我不知道-m的最后一件事。确实非常有用! - ralphtheninja
@Magnus Skog:我用过一两次,所以我不得不去翻阅手册才能再次找到它。 - X-Istence
1
很好!我也不知道这个 :) - James Polley
1
谢谢你为我节省了这么多时间! - Doug Voss

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接