如何更改旧(本地)提交的消息?

时间:2016-02-12 06:36:52

标签: git

我的提交看起来像这样:

alex@alex-M52AD-M12AD-A-F-K31AD:~/node/project$ git log
commit 050e4417634300e724b8e0c29bb8b7a240a9a996
Author: alexcheninfo <alexchen.info@gmail.com>
Date:   Fri Feb 12 12:55:38 2016 +0800

    Rename documents

commit 637bd5ac976118d7e0fb3bf5f2bab950ce0ebb66
Author: alexcheninfo <alexchen.info@gmail.com>
Date:   Fri Feb 12 11:29:01 2016 +0800

    Make sidenav a component

我想更改Make sidenav a component的提交消息。我想过使用git -ammend,但我认为它只能用于更改上一次提交的消息?

3 个答案:

答案 0 :(得分:6)

交互式rebase通常是最简单的方法:git rebase -i 637bd5ac^

这将在每行提交后打开您的编辑器,因为提交在一行上提交。对于每个提交,您可以选择修改方式;选择(保持原样),编辑,重新编辑(仅编辑提交消息),压缩(将该提交与前一个提交组合成一个提交和一个提交消息)或修复(如壁球但忽略第二个提交消息) )。您也可以在执行此操作时重新排序或删除提交。

对于您的问题,您可能希望为相关提交选择“reword”,然后您将有机会编辑该消息。

答案 1 :(得分:2)

git commit -amend只能更改上次提交。您应该使用 git rebase -i 在提交历史记录中选择和编辑您的提交。

答案 2 :(得分:2)

  

我想更改 Make sidenav组件的提交消息   我想过使用git commit -ammend,但我认为它只能用于更改上一次提交的消息?

git commit -ammend

这只会更新您的HEAD,这是最新的提交消息 如果您希望更新链中的其他消息,您有以下几种选择:

交互式rebase = git rebase -i HEAD~X

找到您想要的提交,将选择更改为eedit),然后保存并关闭该文件。

现在当git停止在所需的提交时,使用git commit --amend进行更改。

git filter-branch

这里也有很多选择。 git filter-branch循环提交的集合并以您告诉它的方式更新它们。

例如,您可以使用此行更新所需的字符串:

git filter-branch -f --msg-filter 'sed "s/...//g"' -- --all

或者这个脚本(这个修改提交者数据)

# Loop over all the commits and use the --commit-filter
# to change only the email addresses

git filter-branch --commit-filter '

    # check to see if the committer (email is the desired one)
    if [ "$GIT_COMMITTER_EMAIL" = "<Old Email>" ];
    then
            # Set the new desired name
            GIT_COMMITTER_NAME="<New Name>";
            GIT_AUTHOR_NAME="<New Name>";

            # Set the new desired email
            GIT_COMMITTER_EMAIL="<New Email>";
            GIT_AUTHOR_EMAIL="<New Email>";

            # (re) commit with the updated information
            git commit-tree "$@";
    else
            # No need to update so commit as is
            git commit-tree "$@";
    fi' 
HEAD
相关问题