【问题标题】:How to reword any commit's message given the following message format?给定以下消息格式,如何改写任何提交的消息?
【发布时间】:2019-10-10 16:10:56
【问题描述】:

我需要根据提交 ID 修改提交的消息,而不更改任何其他提交信息。但是,提交消息应该接受换行符等,类似于使用git commit 命令完成的方式。

例如,考虑以下提交

commit <id>
Author: <user-name> <user-email.com>
Date:   ...

    Hello World

我想把提交信息改写成这个

    Hello World

    Text after line break1
    More text

通常的方法是交互式地变基,然后使用git commit --amend 编辑提交或对该提交执行改写操作。但是,这会修改提交者的电子邮件、时间等提交信息。

(检查更新部分是否有变基)

git 中的过滤器分支将允许通过仅更改提交消息和 ID 来重写提交,如 answer 所示。

但是,如何使用 filter-branch 用上述提交消息格式重新编写提交?

更新:

这是交互式变基的示例。

  • 创建一个包含 2 个提交的测试存储库
    test@ubuntu:~/temp_git$ git init
    Initialized empty Git repository in /home/test/temp_git/.git/
    test@ubuntu:~/temp_git$ touch file1
    test@ubuntu:~/temp_git$ git add .
    test@ubuntu:~/temp_git$ git -c "user.name=A" -c "user.email=a@xyz.com" commit -am "Add File1" --author="B <b@xyz.com>"
    [master (root-commit) f122a34] Add File1
     Author: B <b@xyz.com>
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 file1
    test@ubuntu:~/temp_git$ touch file2
    test@ubuntu:~/temp_git$ git add .
    test@ubuntu:~/temp_git$ git -c "user.name=B" -c "user.email=b@xyz.com" commit -am "Add File2" --author="B <b@xyz.com>"
    [master 3b023cf] Add File2
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 file2
    
  • 上面创建的提交有不同的作者和提交者。完整的日志如下:

    test@ubuntu:~/temp_git$ git log --format="fuller"
    commit 3b023cf256ae3498fbaf740329d94842143a5e4a (HEAD -> master)
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:28:07 2019 +0530
    Commit:     B <b@xyz.com>
    CommitDate: Tue Oct 15 08:28:07 2019 +0530
    
        Add File2
    
    commit f122a341e31691f3170207c9a452ff18846fe120
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:27:22 2019 +0530
    Commit:     A <a@xyz.com>
    CommitDate: Tue Oct 15 08:27:22 2019 +0530
    
        Add File1
    
  • 以用户 A 的身份执行交互式变基并改写根提交

    test@ubuntu:~/temp_git$ git -c "user.name=A" -c "user.email=a@xyz.com" rebase -i --root
    [detached HEAD 228b423] Add File1 (test)
     Author: B <b@xyz.com>
     Date: Tue Oct 15 08:27:22 2019 +0530
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 file1
    Successfully rebased and updated refs/heads/master.
    
  • 使用提交者信息的更改更新日志

    test@ubuntu:~/temp_git$ git log --format="fuller"
    commit 0d5e6a5fed5b22fc5f8e310c6e98b1e6b8a821b8 (HEAD -> master)
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:28:07 2019 +0530
    Commit:     A <a@xyz.com>
    CommitDate: Tue Oct 15 08:31:41 2019 +0530
    
        Add File2
    
    commit 228b423e3e511c5954823e42df51a6f6acae91cf
    Author:     B <b@xyz.com>
    AuthorDate: Tue Oct 15 08:27:22 2019 +0530
    Commit:     A <a@xyz.com>
    CommitDate: Tue Oct 15 08:31:28 2019 +0530
    
        Add File1 (test)
    

【问题讨论】:

    标签: git git-filter-branch


    【解决方案1】:

    以下是我的建议:

    1. (首选)不要这样做。不要担心旧的提交消息,只需更好地处理新的提交消息。

    2. filter-branch 您在答案中显示的方式。

    3. 模拟(这仍然会影响提交时间戳):

      1. 将您的 git 配置更改为具有提交原作者的姓名和电子邮件。
      2. git commit --amendgit rebase

    【讨论】:

      【解决方案2】:

      正如here 的回答,可以使用以下脚本使用过滤器分支来改写提交:

      #! /bin/bash
      REV=$1
      MESSAGE=$2
      FILTER="test $(echo '$GIT_COMMIT') = $(git rev-parse $REV) && echo $MESSAGE || cat"
      git filter-branch --msg-filter "$FILTER" -- --all
      

      usage: ./script_name.sh &lt;commit-id&gt; "commit message"

      但是,要在新的提交消息中添加换行符,我必须将提交消息修改为:"$(echo -e 'summary_line\n\nmessage\nmore_message')",如回答 here。 根据commits 的一般约定,上述命令在summary_line 之后添加了两个换行符。

      此外,由于 cmets here 中提到的转义序列字符,必须修改脚本:

      $MESSAGE在这行$(git rev-parse $REV) &amp;&amp; echo $MESSAGE改为\"$MESSAGE\"

      如果脚本名称是reword-commit.sh,那么最后的命令,(将在 git 存储库中运行)

      ./reword-commit.sh <commit-id> "$(echo -e 'Hello World\n\nText after line break1\nMore Text')"
      

      【讨论】:

      • 有没有办法为$MESSAGE 文本指定一个文本文件?此文本文件将包含换行符和撇号。
      • @Mr-IDE 可能这样的事情应该有助于./reword-commit.sh &lt;commit-id&gt; "$(echo -e '$(cat /path/to/file)')"
      • 只是让您知道,这不起作用:./reword-commit.sh &lt;commit-id&gt; "$(echo -e '$(cat /path/to/file)')" - 它给出错误:“cat: /path/to/file: No such file or directory”。 但是这很好用: ./reword-commit.sh ABC123 "$(cat /path/to/file.txt)" 。在单独的说明中,您的答案的第一部分说使用 "echo $MESSAGE" ,但第二部分说使用 "echo \"$MESSAGE\"" 。那么我们应该使用哪一个呢?
      • @Mr-IDE 第一部分只是引用了另一个答案,第二部分讨论了需要进行哪些修改才能使其适用于我的案例。对于这个It gives error: "cat: /path/to/file: No such file or directory" - 可能你没有给出包含该消息的文件的绝对路径。
      【解决方案3】:

      this link

      您可以使用git rebase -i HEAD~N 进行交互式变基,这将显示“N”次提交。 然后,您可以转到您想要更改的提交行并将pick 重命名为reword

      在此之后,您在即将到来的编辑器中调整提交消息。

      [注意]

      不建议更改/rebase 推送的提交,因为您必须 force push 并且其他人必须合并/重新克隆存储库。

      【讨论】:

      • 谢谢你的回答,但是我会要求你再次回答这个问题,我已经提到过交互式变基
      • @SaurabhPBhandari 我知道,但我认为这种变基方式(reword 而不是amend commits)不会改变用户信息。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      相关资源
      最近更新 更多