Skip to content

Instantly share code, notes, and snippets.

@gibatronic
Last active August 21, 2018 13:12
Show Gist options
  • Save gibatronic/cb3d6cc7f707dd356a59976d8285110b to your computer and use it in GitHub Desktop.
Save gibatronic/cb3d6cc7f707dd356a59976d8285110b to your computer and use it in GitHub Desktop.
Prepend the branch name to the commit message
#!/usr/bin/env bash
main() {
local commit_message_file=$1
local branch_name=$(__git_ps1 '%s' | cut -d '|' -f 1)
local commit_message=$(cat "$commit_message_file")
# do nothing if the commit_message already begins with the branch_name
if [[ "$commit_message" == "$branch_name"* ]]; then
return 0
fi
# prepend the branch name to the commit message
printf "%s - %s" "$branch_name" "$commit_message" > "$commit_message_file"
return 0
}
main "$@"

Git commit-msg hook

Prepend the branch name to the commit message.

Useful for workflows where the branch name is an issue number and the commits have to begin with it as well.

Usage

Put the commit-msg file inside your .git/hooks project repository.

Then, for the following commit, on a branch named ABC-1234:

git commit -m 'my thoughtful description'

The commit message will become:

ABC-1234 - my thoughtful description

🍻

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