有没有办法设置回退 git 提交模板?

Is there a way to set a fallback git commit template?

阅读the docs并没有告诉我这是否可以做到,这对我来说表明这是不可能的,但我想请教一些更聪明的人。我认为如果项目模板不存在,则同时指示项目模板 template = .git/gitmessage 和全局模板 template = ~/.gitmessage 以回退到会很有帮助。我尝试在 ~/.gitconfig 中添加 2 个 template 条目,并将一个指向全局模板,一个指向本地模板,但无论我将它们放入什么顺序都不起作用。

相关文档:

When editing the commit message, start the editor with the contents in the given file. The commit.template configuration variable is often used to give this option implicitly to the command. This mechanism can be used by projects that want to guide participants with some hints on what to write in the message in what order. If the user exits the editor without editing the message, the commit is aborted. This has no effect when a message is given by other means, e.g. with the -m or -F options.

Git有两种配置选项:可以有多个值的选项和最多有一个值的选项。我认为commit.template是后者。

最多取一个值的选项的一般模式是范围更窄的配置文件优先于范围更广的配置文件。也就是说,存储库(本地文件)中的 .git/config 优先于 ~/.gitconfig(全局文件),后者又优先于 /etc/gitconfig(系统文件)。

如果你想全局设置这个选项,你可以在你的 ~/.gitconfig 文件中设置,然后在存储库的配置文件中设置一个 project-specific 选项。这应该覆盖全局文件中的值,因此使用 project-specific 模板。对于没有自己模板的项目,您将使用全局文件中定义的模板。