Enhancing Your Git Commit Messages
If you’re reading this chances are that you’re already aware of what Git is and you also work with any ticket-based software, like Jira. In this article, I’m going to show you how automatically prepend the ticket ID you’re working on to your commit message, assuming that you’re using you’re branch names similar to:
feature/ABC-1234-Feature-testing
To achieve this, you’re going to use Git hooks.
Git hooks
According to Git Hooks:
Git hooks are scripts that Git executes before or after events such as: commit, push, and receive. Git hooks are a built-in feature — no need to download anything. Git hooks are run locally.
Now that you know what you need, just create a new file under .git/hooks
folder and name it prepare-commit-msg
.
Copy the code below and paste it into your newly created file.
#!/bin/bash
# List the branches that don't apply bellow
if [ -z "$BRANCHES_TO_IGNORE" ]; then
BRANCHES_TO_IGNORE=(master develop staging test)
fi
# Pick the current branch name and check if it is excluded
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_IGNORED=$(printf "%s\n" "${BRANCHES_TO_IGNORE[@]}" | grep -c "^$BRANCH_NAME$")
# Remove the unnecessary parts
TRIMMED=$(echo $BRANCH_NAME | sed -E -e…