Skip to content

Instantly share code, notes, and snippets.

@DevinXian
Forked from jimschubert/README.md
Created May 8, 2019 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevinXian/85c9a78552bd93b2f8cb60ef7618cf16 to your computer and use it in GitHub Desktop.
Save DevinXian/85c9a78552bd93b2f8cb60ef7618cf16 to your computer and use it in GitHub Desktop.
prepare-commit-msg which adds branch name and description to the end of a commit message (git hook)

Installation

To install globally, copy prepare-commit-msg to /usr/local/share/git-core/templates/hooks and execute:

chmod +x /usr/local/share/git-core/templates/hooks/prepare-commit-msg

To install per-repository, copy prepare-commit-msg to /path/to/repo/.git/hooks/prepare-commit-msg and mark it as executable.

Usage

The branch name will be automatically appended to commit messages using git commit or git commit -m "message".

To include a branch description in your desired branch, execute:

git branch --edit-description

This will open your editor, type a description, save, and exit.

Notes

If you'd like to use this commit message hook in an existing repository, after copying the file to the proper location, run git init within the repository for the hook to be initialized.

#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
# Modified from the stackoverflow answer here: http://stackoverflow.com/a/11524807/151445
#
# Succeed on all merge messages, as evidenced by MERGE_MSG existing
[ -f $GIT_DIR/MERGE_MSG ] && exit 0
# Get branch name and description
NAME=$(git branch | grep '*' | sed 's/* //')
DESCRIPTION=$(git config branch."$NAME".description)
# Don't apply this logic if we are in a 'detached head' state (rebasing, read-only history, etc)
# newlines below may need echo -e "\n\n: (etc.)"
if [ "$NAME" != "(no branch)" ]; then
# Append branch name and optional description to COMMIT_MSG
# For info on parameters to githooks, run: man githooks
echo "\n\n: $NAME $DESCRIPTION" >> "$1"
fi
@DevinXian
Copy link
Author

NAME=$(git branch | grep '*' | awk '{print $2}')

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