#!/bin/sh## git prepare-commit-msg hook for automatically prepending an issue key# from the start of the current branch name to commit messages.# check if commit is merge commit or a commit ammendif[$2="merge"]||[$2="commit"];thenexitfiISSUE_KEY=`git branch | grep -o "\* \(.*/\)*[A-Z]\{2,\}-[0-9]\+"| grep -o "[A-Z]\{2,\}-[0-9]\+"`if[$? -ne 0];then# no issue key in branch, use the default messageexitfi# issue key matched from branch prefix, prepend to commit messageTEMP=`mktemp /tmp/commitmsg-XXXXX`(echo"$ISSUE_KEY: $(cat $1)") > $TEMPcat $TEMP > $1
Comments (11)
Jonathan Doklovic
This commit hook will extract a Jira issue key from your branch name (you are naming your branches with issue key’s aren’t you?) and append it to all commit messages so that many places across our products can glue commits together with issues.
To use this:
make sure the folder(s) ~/.git_template/hooks exists
drop this file in there and make sure it’s named prepare-commit-msg
make ~/.git_template/hooks/prepare-commit-msg executable. (chmod +x)
make sure your ~/.gitconfig contains
[init]
templatedir = ~/.git_template
Now anytime you checkout a repo OR use git init in a directory, the prepare-commit-msg will be copied into your project’s .git/hooks folder.
Note: You can safely run git init within pre-existing git projects to get the file copied over
Brett Taylor
On Windows, it might not be clear where the `~` folder is. Ultimately, this folder is where Git creates your user global .git_config file. You probably have a global config.
git config --global --list --show-origin will show you a list of existing config options and where they were found, including the filename.
Once you found where your .git_config file is, follow the above instructions. I did not need to change the proposed templatedir value in step 4, nor the shebang in the file, but your mileage may vary.
and… if the script isn’t running, double check your settings for core.hooksPath interfering: git config --show-origin core.hooksPath
Joshua JacobsonAccount Deactivated
Hi Jonathan. Works great. I had to add another step between steps 2 and 3 to make ~/.git_template/hooks/prepare-commit-msg executable.
Jonathan Doklovic
Thanks! updated the steps
Abhishek Madhu
I like this a lot. This helps me not waste my time on management and issue tracking.
But for some reason, there is a project that I have to work on in a Windows-10 environment.
Can you please point me in the direction to implement something like this here? It is sad to have to leave this.
Thanks in advance.
Erwin Vrolijk
Instead of creating a temporary file with mktemp I would inline update the draft commit file like this:
#!/bin/sh## git prepare-commit-msg hook for automatically prepending an issue key# from the start of the current branch name to commit messages.# check if commit is merge commit or a commit ammendif[$2="merge"]||[$2="commit"];thenexitfiISSUE_KEY=`git branch | grep -o "\* \(.*/\)*[A-Z]\{2,\}-[0-9]\+"| grep -o "[A-Z]\{2,\}-[0-9]\+"`if[$? -ne 0];then# no issue key in branch, use the default messageexitfi# issue key matched from branch prefix, prepend to commit message
sed -i -e "1s/^/$ISSUE_KEY /"$1
I followed these instructions some time ago but they never seemed to work. So I just gave up and thought I’ll get back to it someday.
But today, for some strange reason I started getting the Jira issue number added to my commit messages.
This turned out to be a big problem though. In this project I am using commitizen and husky. Husky doesn’t like this because the commit message doesn’t start with one of it’s types (eg ‘feat:’, ‘bug:’, etc).
Just thought I’d let you know. I could probably mess around with the script to get it to put the issue number after the type and I might do that when I have a chance
Vu Tung Duong (developer)
does not work anymore.
Jacek Domagalski
can’t run it as well
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.
This commit hook will extract a Jira issue key from your branch name (you are naming your branches with issue key’s aren’t you?) and append it to all commit messages so that many places across our products can glue commits together with issues.
To use this:
[init]
templatedir = ~/.git_template
Now anytime you checkout a repo OR use
git init
in a directory, the prepare-commit-msg will be copied into your project’s .git/hooks folder.Note: You can safely run
git init
within pre-existing git projects to get the file copied overOn Windows, it might not be clear where the `~` folder is. Ultimately, this folder is where Git creates your user global
.git_config
file. You probably have a global config.git config --global --list --show-origin
will show you a list of existing config options and where they were found, including the filename.Once you found where your
.git_config
file is, follow the above instructions. I did not need to change the proposed templatedir value in step 4, nor the shebang in the file, but your mileage may vary.and… if the script isn’t running, double check your settings for core.hooksPath interfering:
git config --show-origin core.hooksPath
Hi Jonathan. Works great. I had to add another step between steps 2 and 3 to make ~/.git_template/hooks/prepare-commit-msg executable.
Thanks! updated the steps
I like this a lot. This helps me not waste my time on management and issue tracking.
But for some reason, there is a project that I have to work on in a Windows-10 environment.
Can you please point me in the direction to implement something like this here? It is sad to have to leave this.
Thanks in advance.
Instead of creating a temporary file with
mktemp
I would inline update the draft commit file like this:
This is fantastic! Thank you for creating this.
To add this automatically to hooks I use NPM
Then modify
package.json
and add the following
More info: https://github.com/bk201-/jira-prepare-commit-msg
I followed these instructions some time ago but they never seemed to work. So I just gave up and thought I’ll get back to it someday.
But today, for some strange reason I started getting the Jira issue number added to my commit messages.
This turned out to be a big problem though. In this project I am using
commitizen
andhusky
. Husky doesn’t like this because the commit message doesn’t start with one of it’s types (eg ‘feat:’, ‘bug:’, etc).Just thought I’d let you know. I could probably mess around with the script to get it to put the issue number after the type and I might do that when I have a chance
does not work anymore.
can’t run it as well