Git 当我压缩 N 次提交时,提交模板不显示

Git commit template does not show up when I squash N-commits

我在我的配置文件中设置了模板文件的路径,当我进行基本提交时它工作正常 (git add -A . && git commit):

[commit]
        template = .git-commit-template.txt

相反,当我尝试压缩 N 次提交并添加它们的消息时,我的提交模板没有显示。

=> 有什么方法可以在压缩最后 N 次提交期间添加 git 模板消息吗?


我在这个答案的帮助下压缩最后 2 次提交的方式:

If you want to start editing the new commit message with a concatenation of the existing commit messages:
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

#!/bin/bash

git reset --soft HEAD~2
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})" # template message does not show up

作为替代方案,当我执行以下操作时,我得到了相同的结果:

git reset --hard HEAD~2 && git merge --squash HEAD@{1} && git commit

您在示例命令中的 -m 参数 覆盖 您的模板。

您可能想要编写一个小脚本:

  • 读取您的模板;
  • 运行s 所需的 git log 命令;
  • 在某个定义的模板位置插入 git log 输出(可能在模板中使用您自己发明的语法);
  • 将生成的文件作为临时文件写入某处;
  • 调用 git commit --edit -F /path/to/temporary/file 而不是 git commit --edit -m ...;
  • 删除临时文件 git commit 本身 returns;
  • git commit 命令的退出状态退出。

但是请注意,git merge --squash 服从 merge.log 配置旋钮,这可能接近您想要的。你可以 运行 git -c merge.log=20 merge --squash ...。缺点是这是 %s 格式,而不是 %B 格式:您只能得到主题行。

我正在寻找如下内容,我需要使用 $(cat .git_commit_template.txt):

打印模板消息

更新代码:

git reset --soft HEAD~2
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1}) $(cat .git_commit_template.txt)"
git push -f