Published on

Shell Function to Retag Git Commit

Authors

On a CI system we use we have it configured so that it only rebuilds and deploys a repos code if that repo is tagged. This is cool in that it lets you have explicit control over when you deploy without restricting people from committing. The main issue is that reusing a git tag (if you are still working on the same version of the app) can be quite cumbersome. So I made the following bash/zsh function which can be added to your .bashrc/.zshrc file and be invoked as follows:

gitretag v1.0

This outputs the following:

Deleting tag v1.0 locally
Deleted tag 'v1.0' (was f864e0f)
Deleting tag v1.0 on remote
To github.com:your-user/your-project.git
 - [deleted]         v1.0
Tag the current head with v1.0 locally
Push v1.0 to remote
Total 0 (delta 0), reused 0 (delta 0)
To github.com:your-user/your-project.git
 * [new tag]         v1.0 -> v1.0
Done...

The shell function you need to add to your rc file is:

function gitretag(){
    tag="$@"
    echo Deleteing tag $tag locally
    git tag -d $tag
    echo Deleteing tag $tag on remote
    git push origin :refs/tags/$tag
    echo Tag the current head with $tag locally
    git tag $tag
    echo Push $tag to remote
    git push origin tag $@
    echo Done...
}