Git: Tagging

Create and delete git tags.

René Kulik on 22.07.2017

Content

Tags?

By using tags certain points in the version history can be marked as important. Usually, release versions inter alia are being tagged.

Create tag

Tags can be easily created, by using the following commands:

$ git tag -a 1.2.3 -m "v1.2.3"
$ git push origin HEAD 1.2.3

With the help of the first command an annotated tag 1.2.3 with the commit message “v1.2.3” will be created. Subsequently the tag will be transmitted to the remote repository and stored as a full objects in the git database.

The following function can be used to simplify the process. Add the function to the bashrc-file and source it afterwards.

createTag() {
    if [ ! $(git rev-parse --is-inside-work-tree 2>/dev/null) ]; then
        echo "You have to be inside a git repository";
        return;
    fi

    if [[ (-z "$1") || ! ($1 =~ ^[0-9]+.[0-9]+.[0-9]+$) ]]; then
        echo "Tag '$1' does not match [0-9]+.[0-9]+.[0-9]+";
        return;
    fi

    git tag -a $1 -m "v$1";
    git push origin HEAD $1;
}

After that the tag 1.2.3 can be created by running the following command:

$ createTag 1.2.3

Make sure to be inside a git repository and the tag comply with the format [0-9]+.[0-9]+.[0-9]+.

Delete tag

In order to delete tags, two commands will suffice. First the tag will be deleted locally and then the adjustment will be pushed to the remote repository.

$ git tag -d 1.2.3
$ git push origin :refs/tags/1.2.3