【问题标题】:How to change the message of an old (local) commit? [duplicate]如何更改旧(本地)提交的消息? [复制]
【发布时间】:2016-05-23 05:31:27
【问题描述】:

我的提交如下所示:

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,但我觉得只能用来改变上次提交的消息?

【问题讨论】:

    标签: git


    【解决方案1】:

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

    这将在每次提交后打开您的编辑器,因为在一行中提到了提交。对于每个提交,您可以选择要如何修改它;选择(保持原样),编辑,改写(仅编辑提交消息),挤压(将该提交与前一个提交合并为一个提交和一个提交消息),或修复(如壁球,但忽略第二个提交消息)。您还可以在执行此操作时重新排序或删除提交。

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

    【讨论】:

    • 637bd5ac^ 是您要编辑的提交的提交 ID(实际上是它的前缀,因为通常这足以唯一),后跟 ^ 字符以选择父级犯罪。我从您的问题中获取了该提交 ID,因此如果您粘贴了存储库的实际输出,那么我写的内容应该可以工作。变基时,您需要选择要变基的提交,因此它将是您要修改的第一个提交之前的那个;这就是我添加^ 来选择父提交的原因。然后你将有机会编辑它之后的任何提交。
    【解决方案2】:

    git commit -amend 只能更改最后一次提交。你应该使用 git rebase -i 在你的提交历史中选择和编辑你的提交。

    【讨论】:

      【解决方案3】:

      我想更改Make sidenav 组件的提交信息。
      本来想用git commit -ammend,但我觉得只能用来改变上次提交的信息?

      git commit -ammend

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


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

      找到你想要的提交,将pick更改为e (edit),然后保存并关闭文件。

      现在,当 git stop 执行所需的提交时,请使用 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
      

      【讨论】:

        猜你喜欢
        • 2021-02-06
        • 2023-03-14
        • 2010-12-25
        • 2016-03-01
        • 2011-02-13
        • 2012-02-08
        • 2017-03-07
        • 1970-01-01
        • 2014-05-24
        相关资源
        最近更新 更多