Tagging
In Git, tagging is the process of assigning a meaningful label or marker to a specific commit in the repository's history. Tags are often used to mark significant points in the project's development, such as releases, versions, or milestones.
Types of Tags
There are two types of tags in Git:
-
Lightweight Tags These are simple pointers to specific commits, similar to branches but immutable. They are created with the
-l
option. -
Annotated Tags These are stored as full objects in the Git database, containing details like the tagger's name, email, date, and a tagging message. They are created with the
-a
option.
Creating Tags
Lightweight Tags
git tag <tag-name> <commit-hash>
git tag v1.0.0 7c52d53
Annotated Tags
git tag -a <tag-name> <commit-hash> -m "Tagging message"
git tag -a v1.0.0 7c52d53 -m "Version 1.0.0 release"
Tagging Later : You can also tag commits after you’ve moved past them.
Viewing Tags
To view all tags in the repository:
git tag
Listing Tags Matching a Pattern
You can use wildcards to list tags matching a specific pattern. Listing tag wildcards requires -l or --list option. The two common wildcards used are:
*
: Matches any sequence of characters.?
: Matches any single character.
Listing Tags Starting with a Prefix
To list tags starting with a specific prefix (e.g., v1
):
git tag -l 'v1*'
Listing Tags with Annotated Tags Only
To list only annotated tags (excluding lightweight tags):
git tag -l -n
Using wildcards with git tag -l
allows you to filter and list tags based on specific patterns, making it easier to manage and navigate through tags in your Git repository. Listing tag wildcards requires -l or --list option.
Pushing Tags to Remote
By default, the git push command doesn’t transfer tags to remote servers. To push a specific tag to the remote repository:
git push origin <tag-name>
To push all tags to the remote repository:
git push origin --tags
git push origin v1.0.0
git push pushes both types of tags.
git push
Deleting Tags
To delete a specific tag:
git tag -d <tag-name>
git tag -d v1.0.0
To delete a tag on the remote repository:
git push origin --delete <tag-name>
git push origin --delete v1.0.0
Checking out Tags
To checkout a specific tag and create a detached HEAD state:
git checkout <tag-name>
git checkout v1.0.0
In “detached HEAD” state, if you make changes and then create a commit, the tag will stay the same, but your new commit won’t belong to any branch and will be unreachable, except by the exact commit hash. Thus, if you need to make changes — say you’re fixing a bug on an older version, for instance — you will generally want to create a branch:
$ git checkout -b version2 v2.0.0
#Switched to a new branch 'version2'
Tagging Best Practices
- Use semantic versioning (e.g., v1.0.0, v1.1.0) for version tags to maintain consistency.
- Annotate tags with meaningful messages describing the purpose or significance of the tagged commit.
- Push tags to the remote repository to share them with collaborators and facilitate release management.
Git tagging is a powerful feature for marking important points in your project's history, making it easier to navigate and manage releases.