Skip to content

Instantly share code, notes, and snippets.

@ivan-osipov
Created January 7, 2024 11:35
Show Gist options
  • Save ivan-osipov/57fc43b9a1192fcd61df1e113e22d19f to your computer and use it in GitHub Desktop.
Save ivan-osipov/57fc43b9a1192fcd61df1e113e22d19f to your computer and use it in GitHub Desktop.
Git Hook "prepare-commit-msg" to add branch name to commit body
#!/bin/sh
# get file path
COMMIT_MSG_FILE="$1"
# get file content
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
# find the current branch name
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
# format extra content for commit message
SUFFIX=$(printf "\n\nBranch: %s" "$BRANCH_NAME")
# handle rebase process and amend commits
if [ "${COMMIT_MSG%"$SUFFIX"}" = "$COMMIT_MSG" ] && [ "$BRANCH_NAME" != "HEAD" ]; then
printf "%s" "$SUFFIX" >> "$COMMIT_MSG_FILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment