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

rre*_*evo 45 git

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

对于配置,一个例子是

$ git config commit.template $HOME/.gitmessage.txt
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*ley 47

这个博客告诉我,如果模板文件的路径不是绝对的,那么路径被认为是相对于存储库根目录.

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

or

git config commit.template relative-path-from-repository-root
Run Code Online (Sandbox Code Playgroud)


rre*_*evo 14

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

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

$ cat .git/commit-msg
My Commit Template
Run Code Online (Sandbox Code Playgroud)

接下来创建一个.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
Run Code Online (Sandbox Code Playgroud)

将新创建的文件标记为可执行文件:

chmod +x .git/hooks/prepare-commit-msg
Run Code Online (Sandbox Code Playgroud)

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


pat*_*cek 7

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

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

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