Wednesday, 10 April 2019

Create git tag on last non merge commit


I have been putting a lot of emphasis on generating change logs using conventional change log. Unfortunately the dev community has gone towards squashing and merging. As a result of this conventional changeling doesn't work of the tag is on a merge commit. This command will generate a tag on the last non merge commit to ensure our changelog works.

First of all we need to get the latest commit that is not a merge commit. This is the main key to this script.

git rev-list --max-parents=1 --max-count=1 HEAD

Now we can use the commit that the above command returns to generate our tag. The git tag command can take a second parameter of a git reference, this will be the commit hash the tag will be created against.

git tag v1.0.0 `git rev-list --max-parents=1 --max-count=1 HEAD`

As with all little commands we can create a script we can put in our bin and run anywhere.

to do Add she'll script