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

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

For configuration an example is

  • How do I shallow clone a repo on a specific branch?
  • Releasing stable version from development versions with GitFlow
  • GitLab specific merge request
  • Jenkins access private BitBucket repo over SSH
  • Git - transferring all commits to another branch and creating a new master
  • Ignoring a directory from a Git repo after it's been added
  • $ git config commit.template $HOME/.gitmessage.txt
    

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

  • Errors using Git and SourceTree
  • Need help to understand merge conflict example
  • Github ERROR: Repository not found (yes, another one)
  • A commit in Git: Is it a snapshot/state/image or is it a change/diff/patch/delta?
  • How to combine group of local commits to single push in IDEA?
  • What's the correct organisation for Go projects using Git (or any VCS)?
  • 3 Solutions collect form web for “How to specify a git commit message template for a repository in a file at a relative path to the repository?”

    This blog tipped 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
    

    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.

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

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

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

    Git Baby is a git and github fan, let's start git clone.