【发布时间】: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