Cover image for Enforce git messages format without throwing everyone off?

Enforce git messages format without throwing everyone off?

piyushkmr profile image Piyush Kumar Baliyan ・3 min read


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

You can use config files to manage aliases e.g. .gitconfig

co = checkout

You can read more about it here

2. Bash Aliases

Bash aliases are another approach similar to creating your own shortcuts (that are even shorter than the above approach).

  • Open ~/.bashrc using vim 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 JIRA status in bash
  • 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
Custom bash prompt

Cool, huh!!

Do the same here as you did above for devhelp.sh

Read Next


Want to study abroad ? Get expert advice and information on colleges, courses, exams, admission, student visa, and application process to study overseas.


markdown guide

git branch --show-current will give you the current branch name.

For "dirtiness" detection you might just run git status --porcelain. If it outputs nothing, the working directory is clean.


Thanks for the git status pointer, that is something new that I learned.
Although I generally don't use dirty check as it add a delay to each terminal prompt.


Some of this can also be done with pre/post-commit hooks in git, which eliminate the need for custom functions that everyone has to remember to use. And if someone uses plain git, it breaks your process, whereas they'd have to modify the hooks to get around it intentionally. Trying to use and involve your whole dev tool stack is a great idea, though. That's why the tools provide integration capability and APIs!

Also, while I applaud your PS1 decoration attempt, it's worth noting that the git contrib source provides a script called git-prompt.sh that defines a __git_ps1 function that does the heavy lifting for you. You can usually find the script in /usr/share/git-core/contrib/completion if you are using a standard git install or wherever you installed git. It can occasionally be slow, though, so YMMV.


That is a good idea to integrate these with git hooks (I already have some hooks setup for linting and type-checking), I can add the API integration as well (that way people won't have to learn new shortcuts). But how can I auto add username to new branch?

And regarding commit message hook, I saw that you can prefill the editor content using git-hooks, but inline modification of commit-message is little tricky to get through (OR I am missing something).

The custom functions I created are just derived from common aliases developers use, an I personally found it faster to type gpoh instead of git push origin HEAD and so on.

Thanks for the __git_ps1, I didn't know that, although I'll not be using the dirty check, as it add a delay to each terminal prompt.