Git Tag represents versions on a branch. A tag is a reference pointing to a specific commit. Let’s understand this by an example. Assume that you are working on a task T1 and commit the task. When you commit the task, you added a tag to that commit with versioning V1. After that, You have finished another task T2, T3 and commit them also. Later on, You have another requirement for task T1 and want to work on that. You can checkout to that particular commit using tag you created earlier on the commit. You can continue the task to make another revision v1.1, v1.2 and so on. Here are some git commands used while working on git tags.
List all tags
Get list of existing tags, you can use following git command:
Search for a tag
When you frequently use git tags, there may be a list which contains hundreds or thousands of tags. To search for a specific pattern, you can use option -l
git tag -l "v1.2.6*"
This will list all available tags like:
Create a new tag
There are two types of tags supported by Git – Light-Weight tags and annotated tags. A lightweight tag is just like a branch which never changed. A light-weight tags is a pointer to a specific commit.
Annotated tags are stored as a object which contains the information like tagger name, email, date and tagging message. Here is the syntax to create annotated tags:
git tag -a <tagName> -m "tagging Message"
This tag will be created on the latest commit. If you forgot to tag any previous commit, you can create a tag for the older commits by using commit hash.
git tag -a tagName -m "tag message" aa562140a5a832bb2de9ec1115fcf60605786152
If you want to push any specific tag than you can push it by the tag name:
git push origin <tagName>
If there are multiple tags to be pushed on git, you can use –tags option to push all tags at once.
git push origin --tags
To delete a local tag, you can use command
git tag -d <tagName>
But delete a tag from local doesn’t delete it from the remote server. To delete a tag from the remote server, you can use
git push origin --delete <tagName>
Checking out Tags – in a detached HEAD state
You can checkout to any tag by tagName.
git checkout <tagName>
Be careful while you checkout to a tag as after checkout to a tag, it will be in a “detached HEAD” state. It means that if you make any changes and commit the changes, commit will not belongs to any branch and unreachable. You can track the commit by the commit hash only. If you want to make any changes, first checkout to tag, create a new branch and then modify the file and make commits. You can do this by following command:
git checkout -b <branchName> <tagName>