Git tags are used to mark specific points in your repository’s history. This is typically done to mark release points (e.g., v1.0, v2.0). There are two types of tags in Git: lightweight and annotated. Here’s how to use them:
Creating Tags
Lightweight Tag:
A lightweight tag is like a branch that doesn’t change. It’s just a pointer to a commit.1
git tag <tagname>
Example:
1
git tag v1.0
Annotated Tag:
Annotated tags are stored as full objects in the Git database. They can have a message, and metadata such as the tagger’s name, email, and date.1
git tag -a <tagname> -m "message"
Example:
1
git tag -a v1.0 -m "Release version 1.0"
Viewing Tags
To list all tags in your repository:
1 | git tag |
To view details of a specific tag:
1 | git show <tagname> |
Example:
1 | git show v1.0 |
Sharing Tags
By default, git push does not push tags to remote repositories. To push a specific tag:
1 | git push origin <tagname> |
Example:
1 | git push origin v1.0 |
To push all tags at once:
1 | git push origin --tags |
Deleting Tags
To delete a local tag:
1 | git tag -d <tagname> |
Example:
1 | git tag -d v1.0 |
To delete a remote tag:
1 | git push origin --delete <tagname> |
Example:
1 | git push origin --delete v1.0 |
Checking Out Tags
To checkout a specific tag (this puts you in a “detached HEAD” state, meaning you are not on a branch):
1 | git checkout <tagname> |
Example:
1 | git checkout v1.0 |
Remember that when you are in a detached HEAD state, any changes you make are not associated with any branch unless you create a new branch from this state.
Summary
- Create a lightweight tag:
git tag <tagname> - Create an annotated tag:
git tag -a <tagname> -m "message" - List tags:
git tag - Show tag details:
git show <tagname> - Push a specific tag:
git push origin <tagname> - Push all tags:
git push origin --tags - Delete a local tag:
git tag -d <tagname> - Delete a remote tag:
git push origin --delete <tagname> - Checkout a tag:
git checkout <tagname>
Using these commands, you can effectively manage tags in your Git repositories.