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

后端 未结 4 1017
春和景丽
春和景丽 2020-12-08 06:23

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

For configuration an example is

$ git config commit.template $HOME/         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 07:03

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

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

    $ cat .git/commit-msg
    My Commit Template
    

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

    #!/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
    

    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.

提交回复
热议问题