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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21998728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:46:55  来源:igfitidea点击:

How to specify a git commit message template for a repository in a file at a relative path to the repository?

git

提问by rrevo

Is there a way to specify a git commit.template that is relative to a repository?

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

For configuration an example is

对于配置示例是

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

But I would like to specify a template file relative to the .git folder of the repository.

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

回答by Dave Neeley

This blogtipped me off that if the path to the template file is not absolute, then the path is considered to be relative to the repository root.

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

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

or

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

回答by rrevo

I used the prepare-commit-msg hook to solve this.

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

First create a file .git/commit-msgwith the template of the commit message like

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

$ cat .git/commit-msg
My Commit Template

Next create a file .git/hooks/prepare-commit-msgwith the contents

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

#!/bin/sh

firstLine=$(head -n1 )

if [ -z "$firstLine"  ] ;then
    commitTemplate=$(cat `git rev-parse --git-dir`/commit-msg)
    echo -e "$commitTemplate\n $(cat )" > 
fi

Mark the newly-created file as executable:

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

chmod +x .git/hooks/prepare-commit-msg

This sets the commit message to the contents of the template.

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

回答by patrickvacek

You can always specify a template at commit-time with -t <file>or --template=<file>.

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

See: http://git-scm.com/docs/git-commit

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

Another option might be to use a prepare-commit-msghook: https://stackoverflow.com/a/3525532/289099

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