Well, if you are lazy as me, or nearly as lazy as me, you must be bored how much steps you have to follow to setup a nice little git commit template to auto populate your ticket number in front and ask for a commit message all the time.
I know, right!!
Okay, lets make it a bit easier.
Before, we continue, let us know about a cool feature of git. In git, you can add a custom command pretty easily. For example, say you want to be able to execute git blah
. Well, the implementation is pretty easy, all you need to do is create a file named git-blah
without any extension, make it executable. e.g. chmod +x /usr/bin/git-blah
and place it in your $PATH variable. Git looks for custom commands in your path and automatically makes them available to you. This works for Windows as well, but you probably don’t need to run the chmod
for the windows.
Well, I did the same thing –
- Created a command file named
git-setup
- Added some codes to it. (I dd get few of those commands from google searching, unfortunately I don’t recall from where 😦 )
- Voila.
I have uploaded the commands to github. So, that someone else lazy as me, stays lazy as me. 😉
So, what to do? –
Make sure this command is available to git
- Clone the repository from here https://github.com/Activehigh/git-commands to any location, that is accessible from command line or bash
- Add the path to your $PATH variable.
- I didn’t want to contaminate my PATH with a repository location, so I used a symlink to connect to a folder name
git-commands
and added that folder to PATH. so that I can change it later pretty easily.
The symlink in windows –
Assumption
The commit message template only works properly with branch naming that follows as hotfix/XXX-999
, feature/XXX-999
, warmfix/XXX-999
, etc formats. You can modify the format if you need. Just edit the code inside the pre-commit-msg
file.
https://github.com/Activehigh/git-commands/blob/master/prepare-commit-msg
#!/bin/sh
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
#/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE"
ISSUE=$(git symbolic-ref HEAD | sed -n -e 's/.*\/\(feature\|hotfix\|bugfix\|warmfix\)\/\([a-zA-Z].*[0-9]\).*/\2/p')
sed -i.bak "1s/^/\[$ISSUE\] /" "$COMMIT_MSG_FILE"
See it in action
It is pretty simple. Go to you favorite git repository and type git setup
. And you are done. It will copy proper file to the git repository.
In addition from the next commit you will see a fancy prefix from your branch ticket number auto added.
Lazy coding!!!!.