DEV Community

Vuyisile Ndlovu
Vuyisile Ndlovu

Posted on • Originally published at vuyisile.com on

Git Tags

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
Enter fullscreen mode Exit fullscreen mode

Types Of Tags

Tags come in two types ;

  1. 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.
  2. 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"
Enter fullscreen mode Exit fullscreen mode

To tag old commits, run:

# To tag a specific commit
git tag -a <commit_hash> -m "Tag message"
Enter fullscreen mode Exit fullscreen mode

Lightweight Tags

To create lightweight tags:

# similar to annotated tags, excpet no -a flag
git tag <commit_hash> -m "Tag message"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To push up multiple tags at once do:

git push origin --tags
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Next, delete the tag(s) from the remote server. There are two ways of deleting tags on a remote server:

  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. Or, using a more intuitive way, use the --delete flag
git push origin --delete <tag name>

# Example
git push origin --delete v1.0
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)