Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 495
rrevo Avatar asked Feb 24 '14 20:02

rrevo


People also ask

How do you specify a commit message to a commit?

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.

What is a git commit template?

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.


2 Answers

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 
like image 122
Dave Neeley Avatar answered Nov 08 '22 22:11

Dave Neeley


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.

like image 37
rrevo Avatar answered Nov 08 '22 20:11

rrevo