How to Tag a Specific Commit in Git: Simple Guide
To tag a specific commit in Git, use
git tag <tagname> <commit-hash>. This creates a tag pointing exactly to the commit you specify by its hash.Syntax
The basic syntax to tag a specific commit is:
git tag <tagname> <commit-hash>: Creates a tag namedtagnameon the commit identified bycommit-hash.git push origin <tagname>: Pushes the created tag to the remote repository.
Explanation: tagname is your chosen label for the commit, and commit-hash is the unique identifier of the commit you want to tag.
bash
git tag <tagname> <commit-hash>
git push origin <tagname>Example
This example shows how to tag a specific commit using its hash and then push the tag to the remote repository.
bash
git tag v1.0.0 9fceb02 git push origin v1.0.0
Output
Total 0 (delta 0), reused 0 (delta 0)
To origin
* [new tag] v1.0.0 -> v1.0.0
Common Pitfalls
- Using a wrong or incomplete commit hash will cause Git to fail tagging.
- Forgetting to push the tag means it stays only in your local repository.
- Trying to tag a commit without specifying the hash tags the current HEAD by default, which might not be what you want.
Wrong way: git tag v1.0.0 (tags current commit, not specific one)
Right way: git tag v1.0.0 9fceb02
bash
git tag v1.0.0 # tags current commit, not specific one git tag v1.0.0 9fceb02 # tags specific commit by hash
Quick Reference
| Command | Description |
|---|---|
| git tag | Create a tag on a specific commit |
| git push origin | Push the tag to remote repository |
| git show | View details of the tagged commit |
| git tag -d | Delete a local tag |
| git push origin :refs/tags/ | Delete a remote tag |
Key Takeaways
Use
git tag <tagname> <commit-hash> to tag a specific commit by its hash.Always push your tags with
git push origin <tagname> to share them remotely.Verify the commit hash before tagging to avoid mistakes.
If you omit the commit hash, Git tags the current commit (HEAD) by default.
Use
git show <tagname> to check which commit a tag points to.