DEV Community

Cover image for Enforce git commit messages automatically
Piyush Kumar Baliyan for AdmitKard

Posted on • Updated on

Enforce git commit messages automatically


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 checkout
$ git config --global branch
$ git config --global commit
$ git config --global status
Enter fullscreen mode Exit fullscreen mode

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

co = checkout
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode
  • 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
  • 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
Enter fullscreen mode Exit fullscreen mode

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

Read Next

Top comments (4)

jingxue profile image
Jing Xue

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.

piyushkmr profile image
Piyush Kumar Baliyan

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.

bloodgain profile image

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 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.

piyushkmr profile image
Piyush Kumar Baliyan

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.