【问题标题】:How to reword a Git commit by its SHA1 hash?如何通过其 SHA1 哈希重写 Git 提交?
【发布时间】:2013-12-26 00:46:17
【问题描述】:

我创建了一个带有我想要修改的提交消息的提交。我还没有发布提交,所以我可以安全地重写历史。我可以使用 git log 找到,所以我知道它的 sha1 哈希。如何快速编辑提交?

【问题讨论】:

    标签: git git-rebase git-commit


    【解决方案1】:

    您可以检查有问题的提交,修改其消息并手动变基回您的分支:

    $ git checkout FIRST_COMMIT_SHA
    $ git commit --amend
    $ git rebase HEAD THE_BRANCH_YOU_CAME_FROM
    

    这个 git 别名会自动执行这个过程:

    reword = "!f() { branch=`git symbolic-ref --short HEAD`; git checkout $1; git commit --amend; git checkout $branch; }; f"
    

    要将其添加到您的~/.gitconfig

    $ git config alias.reword "!f() { branch=`git symbolic-ref --short HEAD`; git checkout $1; git commit --amend; git checkout $branch; }; f"
    

    然后像这样使用:

    $ git reword SHA1_OF_THE_COMMIT_TO_BE_REWORDED
    

    学分:

    【讨论】:

      【解决方案2】:

      另外,修改初始提交消息可以通过使用 rebase 命令并提供--root 标志来完成。此外,您需要指定交互模式并在第一次提交时使用edit,如下所示:

      git rebase -i -root
      
      // Specify 'edit' for the first commit.
      
      // Amend first commit message here.
      git commit --amend
      

      有关--root 标志的更多详细信息,请参阅here

      此外,如果您要修改的消息的提交位于您正在处理的分支中,您也可以通过交互式 rebase 轻松解决该问题。只需找到相应的短 SHA-1 并指定 edit 即可允许修改其提交消息。

      【讨论】:

      • 只有在要改写的提交是根提交时才有效。由于 OP(我)没有说明可能是这种情况,或者不是。
      猜你喜欢
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 2011-01-08
      • 2010-12-26
      • 2018-06-21
      • 2017-10-01
      • 1970-01-01
      相关资源
      最近更新 更多