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

时间:2014-02-24 20:51:37

标签: git

有没有办法指定相对于存储库的git commit.template?

对于配置,一个例子是

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

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

4 个答案:

答案 0 :(得分:41)

This blog告诉我,如果模板文件的路径不是绝对的,那么路径被认为是相对于存储库根目录。

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

or

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

答案 1 :(得分:13)

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

首先使用提交消息的模板创建文件.git/commit-msg,如

$ 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

这会将提交消息设置为模板的内容。

答案 2 :(得分:7)

您始终可以在提交时使用-t <file>--template=<file>指定模板。

请参阅:http://git-scm.com/docs/git-commit

另一种选择可能是使用prepare-commit-msg挂钩:https://stackoverflow.com/a/3525532/289099

答案 3 :(得分:0)

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

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

$ cat << EOF > .git/.commit-msg-template
> My custom template
> # Comment in my template
> EOF

2。编辑项目config文件夹中的.git/文件,以将路径添加到自定义模板。

  • 使用 git 命令:

    $ git config commit.template .git/.commit-msg-template
    
  • 或通过在 config 文件中添加以下几行:

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

等等!

相关问题