【问题标题】:Change commit message for specific commit更改特定提交的提交消息
【发布时间】:2016-08-11 09:37:07
【问题描述】:

注意:与this one 类似的问题,但有一些重要的变化。

给定提交 ID,我有以下函数来重写提交日期:

rewrite-commit-date () {
    local commit="$1"
    local newdate="$2"
    newdate="$(date -R --date "$newdate")"
    echo ">>>> Rewriting commit $commit date: $newdate"
    git filter-branch --env-filter \
        "if test \$GIT_COMMIT = '$commit'
         then
             export GIT_AUTHOR_DATE
             export GIT_COMMITTER_DATE
             GIT_AUTHOR_DATE='$newdate'
             GIT_COMMITTER_DATE='$newdate'
         fi" &&
    rm -fr "$(git rev-parse --git-dir)/refs/original/"
}

我正在尝试实现类似的功能rewrite-commit-message 来更改提交消息。我想要的是:

  1. 函数rewrite-commit-message 接受两个参数:commit_idnew_commit_message
  2. 无需知道旧的提交消息:拥有commit_id 就足以知道要更改哪个提交
  3. git commit --amend,因为这与旧提交有关(不一定与最近的提交有关)
  4. 不用担心重写历史和主仓库:我在一个特性分支工作,我可以这样做git push -f
  5. 我想为此使用filter-branch,但我不知道如何:
    • 将更改应用到特定的提交:rewrite-commit-date 函数中使用的testenv-filter 中使用,但我不会在这里做env-filter,因为我不想更改任何相关的内容提交环境,但提交消息。
    • 如何强制提交消息? --msg-filter 需要原始提交消息。我不关心原始提交消息。有--force-msg-filter 或类似的吗?

我正在寻找的内容类似于this,但有一些注意事项:

  1. 不要将更改应用于一系列提交,而是应用于特定提交
  2. 我不关心原始提交消息,因为我想完全覆盖它

【问题讨论】:

    标签: git git-commit git-rewrite-history git-amend


    【解决方案1】:

    这个小脚本的工作原理如下:

    1. 这会将您的历史从提交改写到分支的尖端。因为你在问题中说这不是问题,所以这符合条件。

    2. 您的提交包含在 master 分支中。您可以通过将分支名称作为另一个参数传递来轻松更改此设置,但提交 更好 在分支中。您可能应该为此构建一些验证,可能使用git rev-parse --abbrev-ref HEADgit branch --all --contains <commit>

    事不宜迟:

    #!/bin/bash
    
    change-commit-msg(){
    
      commit="$1"
      newmsg="$2"
      branch="master"
    
      git checkout $commit
      git commit --amend -m "$newmsg"
      git cherry-pick $commit..$branch
      git branch -f $branch
      git checkout $branch
    
    }
    

    演示

    git init
    echo init > a && git add . && git commit -m "init"
    echo another > a && git commit -am "another"
    echo lastly > a && git commit -am "lastly"
    git log --graph --oneline --all --decorate
    
    * bca608c (HEAD -> master) lastly
    * 4577ab5 another
    * b3d018c init
    
    change-commit-msg 4577ab5 "something else"
    
    * c7d03bb (HEAD -> master) lastly
    * 3ec2c3e something else
    * b3d018c init
    

    【讨论】:

      猜你喜欢
      • 2012-05-09
      • 1970-01-01
      • 2017-11-01
      • 2022-06-10
      • 1970-01-01
      • 2011-05-02
      • 2011-02-13
      • 1970-01-01
      • 2021-02-06
      相关资源
      最近更新 更多