Enforce git commit messages automatically
Piyush Kumar Baliyan
Posted on July 2, 2020
TLDR;
You can enforce git messages by using custom git shortcuts in shell/bash script.
Backdrop and motivation
So, we use JIRA and bitbucket, and the good thing with them is that they integrate very well with each other, e.g.
- it links JIRA issues in your Bitbucket PRs
- you can change Jira status from within Bitbucket
- you can create development branches from inside JIRA issue
But this needs some effort to ensure the consistency in your git messages and branch names, plus if you can follow a certain pattern, it becomes for everyone to collaborate since everyone is following the same set of rules.
Rules we follow
So here are a couple of rules that we follow for branch management and commit messages:
- git branch name
<username>-JIRA-1234[-hotfix|-patch]
- commit message
JIRA-1234 some actually useful commit message
- set a ticket
In Progress
once you start working on it
These are pretty straight forward, and I believe some of you are also following these rules (or at least some variation of it). But sometimes you forget to actually follow these, and this gets caught in PR reviews, or sometimes get merged.
3 solutions
3. Use git aliases
2. Use bash aliases (Easiest)
1. Use own implementation of bash functions (Most powerful & coolest)
3. Git Aliases
Git aliases are cool, and you can create your own shortcuts for common tasks
e.g. Use git co
instead of git checkout
And here is how you can do it yourself.
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
OR
You can use config files to manage aliases e.g. .gitconfig
[alias]
co = checkout
You can read more about it here
https://www.atlassian.com/git/tutorials/git-alias
2. Bash Aliases
Bash aliases are another approach similar to creating your own shortcuts (that are even shorter than the above approach).
- Open
~/.bashrc
usingvim
or any other text editor - Add this to bottom of your file
alias gco="checkout"
alias gbr="branch"
alias gci="commit"
alias gst="status"
- Save it
- Run
source ~/.bashrc
OR restart terminal - run
gco master
and you will see that you are now on master and our command is working.
You can have a look at common aliases here
1. Custom bash functions (Most powerful)
This is the most tricky, but most powerful approach. With this, you can do cool things like:
- auto add JIRA id to commit message
- get JIRA status in your bash terminal
- Directly open PR URL of your current branch in your browser
- and many more
The way I do is, define some env variables like your username, jirapassword, etc, and do a curl request to get status.
You can also append the JIRA ticket number automatically to commit messages using bash functions.
Here is the gist for the same:
- Download this file
wget https://gist.githubusercontent.com/piyushkmr/fe64a2c6dde0b42c0d95f945dbf96fc2/raw/devhelp.sh
- Source it in
~/.bashrc
- run
gh
to get a list of available commands
gcof JIRA-1234 # create a branch with username piyush-JIRA-1234
gpoh # push changes at HEAD right now
gopr # Open the new PR link for your current branch
jira # show Jira status of the current branch
0. BONUS Tip
Use custom bash prompt to get some more space with those big directory names, and show current git branch, like this
Cool, huh!!
Do the same here as you did above for devhelp.sh
Read Next
Posted on July 2, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.