Git tags are a way to mark or label specific commits in git history, usually for denoting release versions.
Viewing Tags
To view tags in a repository, type git tag [--list] [filter]
.
git tag v1.0 v2.0
Types Of Tags
Tags come in two types;
- annotated tags — these are full git objects that contain a reference to the commit, the date, and the tagger’s details like their email address and name.
- lightweight tags — Lightweight tags only contain a pointer to a specific commit and are ideal for local or temporary use.
Creating tags
Annotated Tags
To create annotated tags, type in:
# To tag the latest commit git tag -a tag -m "Tag message" # If you don't supply the -m, git opens up your default editor. # Example: git tag -a v1.0 868c5007e8c7564437 -m "Project Version 1.0"
To tag old commits, run:
# To tag a specific commit git tag -a <commit_hash> -m "Tag message"
Lightweight Tags
To create lightweight tags:
# similar to annotated tags, excpet no -a flag git tag <commit_hash> -m "Tag message"
Sharing tags
Tags are not pushed to upstream servers by default. To push them up, do:
git push origin <tagname> # Example git push origin v1.0
To push up multiple tags at once do:
git push origin --tags
This command transfers tags to the remote server that are not already there.
Deleting Tags
To delete tags locally, do:
git tag --delete <tag number> # Example git tag --delete v1.0
Next, delete the tag(s) from the remote server. There are two ways of deleting tags on a remote server:
- Push an empty or null value to the tag (nothing before the colon), effectively deleting it:
git push origin :refs/tags/<tag name> # Example git push origin :/refs/tags/v1.0
- Or, using a more intuitive way, use the
--delete
flag
git push origin --delete <tag name> # Example git push origin --delete v1.0
Checkout Tags
# Git Checkout to view the tag in a temporary detached head state git checkout v1.0 # To create a new branch based on the tag: git switch -c v1.0 # Alternatively, you can checkout a new branch & base it on the tag. git checkout -b new_branch_name v1.0