Skip to content

Instantly share code, notes, and snippets.

@bartoszmajsak
Last active August 20, 2024 20:28
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
@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?

@aronfiechter
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