Skip to content

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Copy link
Owner Author

@vaporwavie vaporwavie commented Dec 3, 2020

Usage

  • 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