Git Tagging Cheatsheet
Git tags are similar to regular code. One add and deletes tags locally. At some point they are pushed to a remote repository. The syntax is mostly similar to what you do with code.
The best references for git tagging are in the git book, specifically 2.6 Git Basics Tagging and 10.3 Git References. The built-in help git tag --help
is also useful, especially the section on re-tagging.
When reading these, keep in mind that Git has two types of tags. But “tag” almost always means “annotated tag.” The other type of tag, “lightweight tags” might have some use for some tools that need throwaway tags, but in most cases can be ignored. Everything below is for the common case of annotated tags.
Cheatsheet
Action | Command |
---|---|
List All Tags | git tag |
Latest Tag | git describe --tags --abbrev=0 |
Add A Tag Locally | git tag -a TAG -m MESSAGE |
Add a Tag at Specific Commit | git tag -a TAG -m MESSAGE HASH |
Delete a Tag Locally | git tag -d TAG |
Redoing a Tag Locally | git tag -f -a TAG -m MESSAGE |
Push a Tag to Remote | git push origin TAG |
Push all Local Tags to Remove | git push origin --tags |
Delete a Tag on Remote | git push origin :refs/tags/TAG |
List Tags as References | git show-ref --tags |
Retagging and Deleting Remote Tags
Deleting remote tags is not listed in any documentation I have read. The command listed above I found from this post. The git tag --help
has a nice discussion on retagging, but deleting a remote tag is somewhat similar to deleting a remote commit. You can erase a commit, there are sometimes good reasons to do it, but it’s not going to be easy and there is strong possibility of side effects. The same goes for tags.
That said, in most cases you won’t actually need to delete remote tags, unless they are truly just junk. If the tags are for releases, it most likely the tag isn’t bad but the actual release is problem. In which case, make a new release, and a new tag and fail forward. Or your build release system broke. To rekick the system, it’s fine to retag the release using the -f -a
flags, and another git push origin
.