Skip to content

Instantly share code, notes, and snippets.

@bartoszmajsak
Last active March 20, 2024 08:12
Show Gist options
  • Save bartoszmajsak/1396344 to your computer and use it in GitHub Desktop.
Save bartoszmajsak/1396344 to your computer and use it in GitHub Desktop.
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
@zymr-keshav
Copy link

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.

@alexshevch
Copy link

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
Copy link

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
Copy link

Nice one! Thanks!

@iCuiShuai
Copy link

Amazing!
thanks.

@jonnyparris
Copy link

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
Copy link

guuilp 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
Copy link

rsiriani 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
Copy link

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

@jimothyGator
Copy link

jimothyGator 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
Copy link

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
Copy link

loub52 commented Apr 15, 2019

Thanks so much for contributing this hook!

@biblicabeebli
Copy link

this is awesome...

@sohee-b
Copy link

sohee-b 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
Copy link

that is nice.

@vaso123
Copy link

vaso123 commented Sep 29, 2019

Thank you!

@crookse
Copy link

crookse commented Nov 13, 2019

great work!

@guillermocb
Copy link

Thanks, very helpful :)

@tolribeiro
Copy link

tolribeiro commented Jan 27, 2020

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/

Very informative, thanks for that! though I'd suggest that you elaborate a bit on what "making the file executable" means, since not everybody knows. :)

@johncmunson
Copy link

@bartoszmajsak I used this as inspiration to create a githook that prepends commit messages with a Jira ticket number (or some other pattern) if it is present in the branch name. I think this may be more practical for a lot of teams than using the entire branch name.
https://gist.github.com/johncmunson/ca02a8027a923a7f4b2f662c67d6528c

@bartoszmajsak
Copy link
Author

Wow, I'm amazed this little thing lives its own life. Thanks for all the comments and I'm super glad you found it useful. I guess it's time to update my blog with the suggestions :)

thank-you

@Demwunz
Copy link

Demwunz commented May 22, 2020

This is awesome.
Can anyone suggest a way to get this working with husky?

@johncmunson
Copy link

@Demwunz I haven’t tried it out yet, but you might give this a try. Let us know how it goes 👍
https://github.com/bk201-/jira-prepare-commit-msg

@Demwunz
Copy link

Demwunz commented May 22, 2020

@johncmunson, thanks, we're working with Jira now, so I'll just go ahead and use that!

@jankuss96
Copy link

Thanks for the nice script. Does anyone know how to prevent pepending the commit msg if I do a rebase?

@TiredFalcon
Copy link

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.

I just tried this now. It works perfectly on command line, but on GitKraken the commit message becomes somehow empty.

@LenKIM
Copy link

LenKIM commented Oct 14, 2021

It's pretty awesome, I check It working, but I got some issues

image

I don't know why like this warnning show me.

  1 #!/usr/bin/env bash
  2
  3 if [ -z "$BRANCHES_TO_SKIP" ]; then
  4   BRANCHES_TO_SKIP=(master develop release hotfix)
  5 fi
  6
  7 PROJECT_ID=MAT
  8 BRANCH_NAME=$(git symbolic-ref --short HEAD)
  9 BRANCH_NAME="${BRANCH_NAME##*/}"
 10 JIRA_ID=`echo $BRANCH_NAME | egrep -o "$PROJECT_ID-[0-9]+"`
 11
 12 BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
 13
 14 COMMIT_MSG_HEAD=$(head $1 -n 1)
 15 BRANCH_IN_COMMIT=$(grep -c $COMMIT_MSG_HEAD)
 16
 17 if [ -n $JIRA_ID ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
 18   sed -i.bak -e "1s/^/[$JIRA_ID] /" $1
 19 fi

@ocodista
Copy link

Would it be possible to use sed to replace a variable of a commit.template instead of adding at the beginning/end of the commit message?

E.g:

Description:

Ticket: $BRANCH_NAME

@samarnayak
Copy link

bartoszmajsak

Do you have a script that will work on windows machines?

@bartoszmajsak
Copy link
Author

@samarnayak where do you want to run it? Under Powershell?

Alternatively, under WSL or (even) Cygwin things should work fine. But I have to admit I haven't tried it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment