Skip to content

Instantly share code, notes, and snippets.

@abalejr
Created July 8, 2022 14:57
Show Gist options
  • Save abalejr/6d3183b4c0acb8bbcc701e1dfe6d6ddb to your computer and use it in GitHub Desktop.
Save abalejr/6d3183b4c0acb8bbcc701e1dfe6d6ddb to your computer and use it in GitHub Desktop.
Append Issue Number to Commit Message
#!/bin/bash
FILE=$1
MESSAGE=$(cat $FILE)
TICKET=$(git rev-parse --abbrev-ref HEAD | grep -Eo '^(\w+/)?(\w+[-_])?[0-9]+' | grep -Eo '(\w+[-])?[0-9]+' | tr "[:lower:]" "[:upper:]")
if [[ $TICKET == "" || "$MESSAGE" == "$TICKET"* ]];then
exit 0;
fi
echo "$MESSAGE (#$TICKET)" > $FILE
@abalejr
Copy link
Author

abalejr commented Jul 8, 2022

Works by including the issue number in your branch name.
Put it in your project in the .git/hooks folder

If you wanna install it by default on any repo you do git init on, you can add it to ~/.git-templates/hooks. Just create the folders if they don't exist.

Example:
Commits made to a branch called 69420_to_the_moon will have (#69420) appended to the message automagically. Yes, there is a space before the opening parenthesis. So if you do git commit -m "Buy the dip", your message will be Buy the dip (#69420).

GitHub will automatically link your commits to the issue, showing them in the timeline of the issue and you'll be able to hover over the link when looking at the commit history and see a tooltip for the issue.

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