如何在与存储库相对路径的文件中为存储库指定git提交消息模板?

如何在与存储库相对路径的文件中为存储库指定git提交消息模板?,git,Git,是否有方法指定相对于存储库的git commit.template 对于配置,例如 $ git config commit.template $HOME/.gitmessage.txt 但是我想指定一个相对于存储库的.git文件夹的模板文件。您可以在提交时使用-t或-template=指定模板 见: 另一个选项可能是使用prepare-commit-msghook:我使用prepare-commit-msg钩子来解决这个问题 首先创建一个文件.git/commit msg,其中包含commit

是否有方法指定相对于存储库的git commit.template

对于配置,例如

$ git config commit.template $HOME/.gitmessage.txt

但是我想指定一个相对于存储库的.git文件夹的模板文件。

您可以在提交时使用
-t
-template=
指定模板

见:


另一个选项可能是使用
prepare-commit-msg
hook:

我使用prepare-commit-msg钩子来解决这个问题

首先创建一个文件
.git/commit msg
,其中包含commit消息的模板,如

$ cat .git/commit-msg
My Commit Template
接下来创建一个包含内容的文件
.git/hooks/prepare commit msg

#!/bin/sh

firstLine=$(head -n1 $1)

if [ -z "$firstLine"  ] ;then
    commitTemplate=$(cat `git rev-parse --git-dir`/commit-msg)
    echo -e "$commitTemplate\n $(cat $1)" > $1
fi
将新创建的文件标记为可执行文件:

chmod +x .git/hooks/prepare-commit-msg
这会将提交消息设置为模板的内容。

提示我,如果模板文件的路径不是绝对路径,则认为该路径相对于存储库根

git config commit.template /absolute/path/to/file

or

git config commit.template relative-path-from-repository-root

1。在项目目录中使用自定义模板创建文件。

在本例中,在项目的
.git/
文件夹中:

$ cat << EOF > .git/.commit-msg-template
> My custom template
> # Comment in my template
> EOF
  • 或者通过在配置文件中添加以下行:

    [commit]
      template = .git/.commit-msg-template
    

  • 注意:对于那些想要相对于主页的用户,
    git-config-commit.template~/.gitmessage.txt
    被记录为从1.9开始在
    man
    上工作,最好使用
    $home
    ,因为即使更改用户名,您也可以使用相同的文件。需要有人修补手册页以更好地解释该选项:-)当我挤压提交@rrevo时,模板不会显示
    [commit]
      template = .git/.commit-msg-template