commit.template在"merge--squash"时未使用



commit.template中的消息模板仅用于提交。正因为如此,当我做git merge --squash mybranch && git commit -a时,它不会被使用。

有没有类似merge.template或其他解决方案的东西,我可以在进行合并时修改编辑器中显示的默认消息?

我在通常的承诺中看到的

当进行git commit -a:时,这会在我选择的编辑器中打开

This is my commit message template set via "git config commit.template".
# Bitte geben Sie eine Commit-Beschreibung für Ihre Änderungen ein. Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch main
# Ihr Branch ist auf demselben Stand wie 'origin/main'.
#
# Zum Commit vorgemerkte Änderungen:
#       geändert:       README.md
#
# Unversionierte Dateien:
#       my-commit-msg-tempalte

git merge --squash之后我看到了什么

这出现在我选择的git merge --squash myfix && git commit -a编辑器中。模板消息不存在!

Squashed commit of the following:
commit 9474d601e6b3ed33efc432f623c08e7931095ba0
Author: buhtz <c.buhtz@posteo.jp>
Date:   Mon Mar 13 09:22:33 2023 +0100
commit three
commit 2f6524da10a6c3fba8963761abcd3b5ba3d19f12
Author: buhtz <c.buhtz@posteo.jp>
Date:   Mon Mar 13 09:22:25 2023 +0100
commit two
commit 5520ea8e2e75a29e06322a742a4b40dbbb74b3ee
Author: buhtz <c.buhtz@posteo.jp>
Date:   Mon Mar 13 09:22:17 2023 +0100
commit one
# Bitte geben Sie eine Commit-Beschreibung für Ihre Änderungen ein. Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch main
# Ihr Branch ist 1 Commit vor 'origin/main'.
#   (benutzen Sie "git push", um lokale Commits zu publizieren)
#
# Zum Commit vorgemerkte Änderungen:
#       geändert:       README.md
#       geändert:       spiel.py
#
# Unversionierte Dateien:
#       my-commit-msg-tempalte

我想要什么

在完成git merge --squash myfix && git commit -a之后,我希望我的编辑器在模板所在的地方能想出这样的东西。压缩提交消息很好,但对我来说不是强制性的。

This is my commit message template set via "git config commit.template".
Squashed commit of the following:
commit 9474d601e6b3ed33efc432f623c08e7931095ba0
Author: buhtz <c.buhtz@posteo.jp>
Date:   Mon Mar 13 09:22:33 2023 +0100
commit three
commit 2f6524da10a6c3fba8963761abcd3b5ba3d19f12
Author: buhtz <c.buhtz@posteo.jp>
Date:   Mon Mar 13 09:22:25 2023 +0100
commit two
commit 5520ea8e2e75a29e06322a742a4b40dbbb74b3ee
Author: buhtz <c.buhtz@posteo.jp>
Date:   Mon Mar 13 09:22:17 2023 +0100
commit one
# Bitte geben Sie eine Commit-Beschreibung für Ihre Änderungen ein. Zeilen,
# die mit '#' beginnen, werden ignoriert, und eine leere Beschreibung
# bricht den Commit ab.
#
# Auf Branch main
# Ihr Branch ist 1 Commit vor 'origin/main'.
#   (benutzen Sie "git push", um lokale Commits zu publizieren)
#
# Zum Commit vorgemerkte Änderungen:
#       geändert:       README.md
#       geändert:       spiel.py
#
# Unversionierte Dateien:
#       my-commit-msg-tempalte

虽然一些Git存储库远程托管服务,如GitLab,可能会提供适当的合并消息模板,但您需要一个本地挂钩来修改合并消息。

例如,prepare_commit_msg本地挂钩(Git 2.24+,2019年第4季度)

它需要一到三个参数。

  • 第一个是包含提交日志消息的文件的名称
  • 第二个是提交消息的来源,可以是:
    • message(如果提供了-m-F选项)
    • template(如果给出了-t选项或者设置了配置选项commit.template)
    • 或:
      • merge(如果提交是合并或存在.git/MERGE_MSG文件)
      • squash(如果存在.git/SQUASH_MSG文件);或
      • commit,然后是提交对象名称(如果给定了-c-C--amend选项)

在这个钩子中,您可以(当第二个参数是mergesquash时)编写一个脚本,将merge.template与您当前的挤压消息相结合。

  • 模板的内容在git config commit.template引用的文件中
  • 为了修改提交消息而更新的文件是.git/MERGE_MSG.git/SQUASH_MSG(在您的情况下:后者)

但是修改.git/SQUASH_MSG并退出0,由git merge --squash创建的合并压缩提交将集成提交模板。


明确这一点。我不想修改提交或提交消息。我想在完成合并压缩和提交后,让我的编辑器打开提交消息模板。

那么你的prepare_commit_msg钩子可以像一样简单

#!/bin/bash
if [[ ! -e ".git/SQUASH_MSG" ]]; then exit 0; fi
# append the commit template after the default squash message
cat "$(git config commit.template)" >> ".git/SQUASH_MSG"
$EDITOR ".git/SQUASH_MSG"

这将把你的模板放在正在准备的提交消息中,然后打开编辑器
编辑器关闭后,修改后的.git/SQUASH_MSG将用于最终提交
在这种情况下,不需要git commit -a

如果你不介意再键入几个单词,你可以试试这个,

git commit -a -F $(git config --get commit.template) --edit

或较短形式的

git commit -a -F $(git config commit.template) -e

-F在不进行编辑的情况下从以下文件中读取提交消息。-e允许进一步编辑消息。当然,您也可以使用真实的模板路径。

您可以使用别名或bash函数来包装它。我更喜欢可执行的自定义git命令。我使用git-bash,所以我会在/usr/bin中的git-bash安装路径下创建一个bash脚本,例如git-cm,并使其可执行。

#!/bin/bash
git commit -a -F $(git config commit.template) -e

然后我可以使用git cm来调用脚本。如果未设置commit.template,则该命令将失败,不会产生任何不良副作用。

执行git merge --squash时,会将包含新提交消息的文件写入.git/SQUASH_MSG,该文件优先于您的提交模板,并且没有任何选项可以覆盖该文件。

您可以在进行合并后简单地删除该文件。

有很多方法可以帮助您删除该文件,但最简单的方法是简单地创建一个别名:

git config --global alias.my-merge-squash 
'!f() { git merge --squash $@ && rm -f .git/SQUASH_MSG; }; f'

现在您可以执行git my-merge-squash mybranch而不是git merge --squash mybranch

你可以给你的别名起任何你喜欢的名字,比如ms

但是,如果您通常在每次合并后执行git commit -a,则可以配置一个别名来同时执行所有操作:

git config --global alias.msc 
'!f() { git merge --squash $@ && rm -f .git/SQUASH_MSG && git commit -a; }; f'

应该可以为挤压消息预先准备模板,但这需要编写一个脚本并将其添加到$PATH中。如果你对此感兴趣,请在评论中告诉我。

我相信您可以通过在.git/hooks中创建一个prepare-commit-msg钩子文件来获得这些结果。执行git merge --squash mybranch && git commit -a时,编辑器将打开原始挤压消息,然后是提交模板:

#!/bin/bash
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
if [ "$COMMIT_SOURCE" = "squash" ]; then
cat "$(git config commit.template)" >> $COMMIT_MSG_FILE
fi

通常,当存在.git/SQUASH_MSG文件时,prepare msg钩子的COMMIT_SOURCE仅为"0";南瓜";。在正常提交期间,commit_SOURCE是您的模板文件。这就是没有使用提交模板的原因。

为了使用您的模板进行挤压提交,您需要将提交模板的内容添加到commit_MSG_FILE中。

最新更新