Smart commit with git alias

Smart commit with git alias

Problem?

git commit -am ‘JIRA-1234 draft for feature abc’

All of us know linking JIRA id with each commit can help in tracking and other use cases. But many of us usually find it tricky to remember which JIRA ID they are currently working on.


Some of us realize which JIRA ID they currently work is actually the present in the branch name itself. And they google about setting that up.

And they come up with a solution to add a git pre commit hook that fetches the JIRA ID from the branch name and prepends it into the commit message.


So effectively it resolves the problem of remembering JIRA ID.

But what if you have 10s of repos to work on? Will you configure pre pre-commit hook in each one of them?


Then one will think about a global pre-commit hook.

But how about you also don’t have to type “commit -am” everytime you make a commit. Saving about 2 seconds everytime you make commit..

Solution

We can use the git alias to resolve all of these problems, via git config. That means you can just type

git c ‘draft for feature abc’

Which produces commit message as

JIRA-1234 draft for feature abc


Git alias you need to add

Here is my `~/.gitconfig` looks like.

.
.
.

[alias]
  # commit shortcut
  c = "!f() { git commit -am \"`git curr-issue` $1\"; }; f"
  # finding current branch you are working on
  curr-branch = rev-parse --abbrev-ref HEAD
  # finding current issue you are working on, taken from branch
  curr-issue = !echo $(git curr-branch) | awk -F - '{print toupper($1)\"-\"$2}'

.
.
.

Please note : You might want to tweak above aliases as per your branch name structure. In my case, all my branches have structure `jira-1234-abc-def`.

All i wanted to give an idea how git alias can solve problem of prepending bug id in your commits, across repos. perhaps in a way better than pre-commit-hook.

Well there are much more in my ~/.gitconfig. I will write about them later.


Pankaj Jangid

Building #AlphaFi #Crypto #DeFi

4 å¹´

Nice useful trick.

要查看或添加评论,请登录

Ramesh K.的更多文章

社区洞察

其他会员也浏览了