Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Git Hook that appends your branch name within the commit message.
BRANCH_NAME=$(git branch 2>/dev/null | grep -e ^* | tr -d ' *' | sed 's:.*/::')
if [ -n "$BRANCH_NAME" ]; then
echo "$BRANCH_NAME: $(cat $1)" > $1

This comment has been minimized.

Copy link
Owner Author

@vaporwavie vaporwavie commented Dec 3, 2020


  • Inside your .git/hooks directory, rename prepare-commit-msg.sample to prepare-commit-msg
  • Open prepare-commit-msg and delete all of its content
  • Paste this script
  • :wq

Possible scenarios:

Be aware that this script expects this pattern: [BRANCH_TYPE]/[TICKET], where only [TICKET] will be appended.

  • git commit will output "BRANCH_NAME: [whitespace]" inside your git preferred editor
  • git commit -[a]m "YOUR_MESSAGE" will output "BRANCH_NAME: YOUR_MESSAGE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment