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/.gitmessage.txt
But I would like to specify a template file relative to the .git folder of the repository.
The easiest way to create a Git commit with a message is to execute “git commit” with the “-m” option followed by your commit message. When using the Git CLI, note that you should restrict your commit message in order for it not to be wrapped.
commit. template allows you to “specify the pathname of a file to use as the template for new commit messages”, which basically means that you can save your commit message template as a file in a project and then configure Git to use it every time you git commit your changes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With