【问题标题】:How to specify a git commit message template for a repository in a file at a relative path to the repository?如何在存储库的相对路径的文件中为存储库指定 git 提交消息模板?
【发布时间】:2014-03-26 18:18:41
【问题描述】:

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

配置示例是

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

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

【问题讨论】:

  • 注意:对于那些想要相对于家的人,git config commit.template ~/.gitmessage.txt 被记录为在 1.9 中可以在 man 上工作,并且比使用 $HOME 更可取,因为您可以使用相同的文件即使您更改用户名。

标签: git


【解决方案1】:

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

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

or

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

【讨论】:

  • 有人需要修补手册页以更好地解释该选项:-)
【解决方案2】:

我使用了 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

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

【讨论】:

  • 当我压缩提交@rrevo 时模板不显示
【解决方案3】:

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

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

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

【讨论】:

    【解决方案4】:

    1.在项目目录中使用您的自定义模板创建一个文件。

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

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

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

    • 使用 git 命令:

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

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

    等等!

    【讨论】:

      猜你喜欢
      • 2011-10-30
      • 1970-01-01
      • 2011-08-08
      • 2012-04-30
      • 2015-09-14
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      相关资源
      最近更新 更多