Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to automatically prepend git commit with a branch name
#!/bin/bash
# This way you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
fi
@bartoszmajsak

This comment has been minimized.

Copy link
Owner Author

commented Nov 26, 2011

Rename .git/hooks/prepare-commit-msg.sample to prepare-commit-msg, paste the script and make the file executable.

For instance with branchARQ-653
$ git commit -m"Fixed bug"
will result with commit "[ARQ-653] Fixed bug"

More elaborated way with the reasoning behind this can be found on my old and dusty blog http://blog.bartoszmajsak.com/blog/2012/11/07/lazy-developers-toolbox-number-1-prepend-git-commit-messages/

@bartoszmajsak

This comment has been minimized.

Copy link
Owner Author

commented Dec 13, 2011

Updated script to not include [master] in the commit message when working on this branch.

@bartoszmajsak

This comment has been minimized.

Copy link
Owner Author

commented Nov 25, 2013

Updated script to use better commands based on stackoverflow hints

@bartoszmajsak

This comment has been minimized.

Copy link
Owner Author

commented Sep 18, 2014

Script handles now branching name which is for example used in git-flow. Eg for branch 'feature/ABC-399' it will extract ABC-399

@the-glima

This comment has been minimized.

Copy link

commented Jun 24, 2015

Thanks, man.

This is exactly what I need it.

@bserem

This comment has been minimized.

Copy link

commented Oct 16, 2015

Can the "skipped" branches include wildcards?
It would be really cool to omit stageXX (so, anything that start with stage).

Thanks for the script ;)

@r3dDoX

This comment has been minimized.

Copy link

commented Jun 8, 2016

Tried to write this myself after leaving NEO. Your solution is way nicer than mine :)

@arnolox

This comment has been minimized.

Copy link

commented Jun 30, 2016

Thanks for sharing this! Others who only want this script to run when git commit is called with no parameters can try wrapping this script in the if statement featured here
In particular :

if [ x = x${2} ]; then
  #script here
fi

Prevents the branch code from being triggered if any parameter was passed to git commit e.g -m or --amend (if length of parameter 2 is > 0)

@psihonavt

This comment has been minimized.

Copy link

commented Jul 12, 2016

Thanks for this hook.

@guyarad

This comment has been minimized.

Copy link

commented Sep 4, 2016

One important note (which occurred to me too) - git symbolic-ref --short HEAD will miserably fail during rebase. This is because during rebase, the HEAD is detached, and doesn't have a symbolic ref. See this for details on fixing it.

@bartoszmajsak

This comment has been minimized.

Copy link
Owner Author

commented Oct 5, 2016

@arnolox @guyarad thanks for the suggestions! :) I guess GH doesn't notify me about any life going on here! I will refine it.

@bartoszmajsak

This comment has been minimized.

Copy link
Owner Author

commented Oct 5, 2016

@r3dDoX it's mature and refined several times :) Glad to see you around!

@nioe

This comment has been minimized.

Copy link

commented Jun 19, 2017

Cheers mate! :)

@meherranjan

This comment has been minimized.

Copy link

commented Sep 25, 2017

Neat little gist

@madeindjs

This comment has been minimized.

Copy link

commented Nov 8, 2017

For a quick install, into your project folder enter this command line:

$ curl https://gist.githubusercontent.com/bartoszmajsak/1396344/raw/bff6973325b159254a3ba13c5cb9ac8fda8e382b/prepare-commit-msg.sh > .git/hooks/prepare-commit-msg && chmod u+x .git/hooks/prepare-commit-msg
  • get Gist
  • copy into .git/hooks/prepare-commit-msg file
  • make it executable
@dvejmz

This comment has been minimized.

Copy link

commented Dec 1, 2017

Finally a hook that works! Thanks a lot for this.

@gezzi

This comment has been minimized.

Copy link

commented Dec 5, 2017

I have many repos. Is there a way to do this at a higher level, instead of one repo at a time?

@mikevrind

This comment has been minimized.

Copy link

commented Dec 15, 2017

What would I need to change to get a complete branch name in the commit message?
I always prepend the branch name with 'feature/', 'bugfix/', 'unittest/' etc. These prefixes are beeing filtered away, probably by "BRANCH_NAME="${BRANCH_NAME##*/}"". If I remove this line, the hook fails. Any tips?

@inarli

This comment has been minimized.

Copy link

commented Dec 15, 2017

Thank you. It is very useful!

@bonorumetmalorum

This comment has been minimized.

Copy link

commented Jan 25, 2018

@gezzi you can paste this bash script into a hooks folder in your directory of choice.

you can then run 'git config --global core.hooksPath /path/to/my/centralized/hooks' where /path/to/my/centralized/hooks is the hooks folder you created containing this script.

read more about it here: https://stackoverflow.com/questions/2293498/git-commit-hooks-global-settings

@zymr-keshav

This comment has been minimized.

Copy link

commented Jan 29, 2018

I have added this script in mac os 10.13.1 and it show the branch name appenede when do git commit -am ".." but on github. I cannot see the branch name in commit message.

@shevaroller

This comment has been minimized.

Copy link

commented Feb 14, 2018

I had a similar use case, but instead of skipping certain branches I needed to only include branches that start with certain characters.

Here is my version of the hook: https://gist.github.com/shevaroller/680719f31e610cff3e6d8c930a078eb0

@Abushawish

This comment has been minimized.

Copy link

commented May 17, 2018

Thanks man! Note to other newbies like me:

  • Making the file "executable", for Mac at least, means doing "chmod u+x .git/hooks/prepare-commit-msg" (when you're at the root of the project).
  • You will need to quit the terminal, re-open it, then commit for the script to work.
@dpapageo

This comment has been minimized.

Copy link

commented May 31, 2018

Nice one! Thanks!

@csIng

This comment has been minimized.

Copy link

commented Aug 16, 2018

Amazing!
thanks.

@jonnyparris

This comment has been minimized.

Copy link

commented Aug 21, 2018

This is great, thanks a lot!
I forked and added a grep for the JIRA at the end of the branch name: https://gist.github.com/jonnyparris/0a10cc63af281de23a4fd34116fed3e6

@guuilp

This comment has been minimized.

Copy link

commented Aug 22, 2018

Does anyone here uses GitKraken? I tried to use this hook but it isn't appending the branch name to the commit when commiting from GitKraken.

@rsiriani

This comment has been minimized.

Copy link

commented Dec 6, 2018

Hello,

I must be doing something wrong. I'm trying to use your script, as is. But when I try to commit, I get the following error:

": bad flag in substitute command: 'E'

  1. I'm using git flow. My feature branch name is: feature/ABC-123-some-cool-feature
  2. I'm on mac os

Any thoughts?

thanks

@milind-shakya-sp

This comment has been minimized.

Copy link

commented Jan 3, 2019

https://github.com/milin/giticket might help if you are using precommit.

@jimothyGator

This comment has been minimized.

Copy link

commented Jan 24, 2019

@rsirani: This works on macOS (tested on Mojave):

(Edit: My script below was based on an old version of this Gist)

#!/bin/sh

BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2> /dev/null | grep -oE "[A-Z]+-[0-9]+")
if [ -n "$BRANCH_NAME" ]; then
    echo "[$BRANCH_NAME] $(cat $1)" > $1
fi

It will only include the ABC-123 portion of your branch name, so for feature/ABC-123-some-cool-feature, it will prepend [ABC-123] to the comment.

Also, it will only append the branch name if it follows that patter (ABC-123), so master, develop, etc. are automatically excluded.

This does mean if your ticket system follows a different convention (e.g., it doesn't begin with uppercase letters), this won't append the ticket number. But this works well for people using JIRA.

@wingleung

This comment has been minimized.

Copy link

commented Mar 21, 2019

did some modifications a while ago that:

  • prevent certain branches from prepending the jira key
  • only prepend the jira key if there isn't any jira key in the branch name

https://gist.github.com/wingleung/1b6efce388b81f5554ca

@loub52

This comment has been minimized.

Copy link

commented Apr 15, 2019

Thanks so much for contributing this hook!

@biblicabeebli

This comment has been minimized.

Copy link

commented May 17, 2019

this is awesome...

@sohee-b

This comment has been minimized.

Copy link

commented Jul 19, 2019

@mikevrind

I changed the / to % in the sed command and it worked for me.
give it a try:

#!/bin/bash

# This way you can customize which branches should be skipped when
# prepending commit message. 
if [ -z "$BRANCHES_TO_SKIP" ]; then
  BRANCHES_TO_SKIP=(master develop test)
fi

BRANCH_NAME=$(git symbolic-ref --short HEAD)

BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)

if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then 
  sed -i.bak -e "1s%^%[$BRANCH_NAME] %" $1
fi
@automagiq

This comment has been minimized.

Copy link

commented Aug 29, 2019

that is nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.