Enforce git commit messages automatically
Tags: git, bash, github, productivity
Published: 2020-07-02 09:30:10


### 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.&nbsp;Use git aliases
2.&nbsp;Use bash aliases (Easiest)
1.&nbsp;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.
```bash
$ 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`
```bash
[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` using `vim` or any other text editor
- Add this to bottom of your file
```bash
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
{% gist https://gist.github.com/piyushkmr/fe64a2c6dde0b42c0d95f945dbf96fc2 file=gitbash_aliases.sh %}

## 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](https://dev-to-uploads.s3.amazonaws.com/i/iavoyoizl43rbi4o40yi.png)
- 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
```bash
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
```
{% gist https://gist.github.com/piyushkmr/fe64a2c6dde0b42c0d95f945dbf96fc2 file=devhelp.sh %}

### 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](https://dev-to-uploads.s3.amazonaws.com/i/h1ooddhf7vkfca6vs90x.png)

> Cool, huh!!

Do the same here as you did above for `devhelp.sh`
{% gist https://gist.github.com/piyushkmr/fe64a2c6dde0b42c0d95f945dbf96fc2 file=custom_bash.sh %}

#### Read Next
{% link https://dev.to/admitkard/mobile-issue-with-100vh-height-100-100vh-3-solutions-3nae %}