空的合并提交可以让我重新合并

时间:2018-07-04 08:53:34

标签: git

我有一个staging分支,出于错误,我从feature/amazing得到了一个空的合并提交。
现在我想在feature/amazing中合并staging,但是由于git告诉我这是最新的,所以我不能。我该怎么办?

2 个答案:

答案 0 :(得分:1)

如果您希望摆脱不必要的合并提交,可以这样做

git reset --hard <sha of the commit you want to point the HEAD to> 
在暂存分支中的

。 通常是合并提交下的提交。您可以通过git log来获取它。

--hard将丢失上述提交中的所有更改,因此请明智地使用它,也可以使用--soft。 然后,您可以在feature/amazing分支中进行修改,然后再次合并。

答案 1 :(得分:0)

是的,绝对不能重新合并它,因为您已经合并了它,并创建了一个“合并提交”。但是您的问题是您创建了一个空的提交消息。

您可以通过运行以下命令,用一个新的空消息替换空的提交消息。

git commit --amend -m 'your new commit message'

Git manual page解释了此功能:

--amend
    Replace the tip of the current branch by creating a new commit. The
    recorded tree is prepared as usual (including the effect of the -i and
    -o options and explicit pathspec), and the message from the original
    commit is used as the starting point, instead of an empty message, when
    no other message is specified from the command line via options such as
    -m, -F, -c, etc. The new commit has the same parents and author as the
    current one (the --reset-author option can countermand this).


    It is a rough equivalent for:

        $ git reset --soft HEAD^
        $ ... do something else to come up with the right tree ...
        $ git commit -c ORIG_HEAD

    but can be used to amend a merge commit.

此命令等效于重新设置并再次提交您的工作,但仅需执行一个命令。它实际上将您的新提交消息和最后一个提交消息结合在一起以创建一个新的提交(意味着,您将获得一个新的哈希)。另外,如果您已上演更改,这些更改也将添加到新提交中。

如果您已经将空提交推送到远程,则您真的应该担心重写历史记录。

希望对您有所帮助:)随时询问您是否想了解更多信息。