Git Tags: All you need to know!

Alish Giri - Oct 19 - - Dev Community

Git has the ability to tag specific points in a repository’s history. This is useful to mark releases (like v1.2 or v2.0).

What you will learn?

  • Types of git tags and how to create them?
  • List existing git tags.
  • How to delete a git tag?
  • How to move the last tag to the latest commit?
  • How to edit a tag message?
  • How to get the code from a specific tag?

Types of git tags and how to create them?

There are two types of git tags: lightweight and annotated.

Most of the time you will use Annotated tags. A Lightweight tag is mostly used for private purposes.

Annotated Tags

This stores information such as tagger name, email, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG).

git tag -a v1.9 -m "your release information..." -s
# -a = annotated
# -m = message
# -s = signed

# Use this to list detailed information that the tag stores.
git show v1.9
Enter fullscreen mode Exit fullscreen mode

Lightweight Tags

Do not use -a, -s, or -m to apply lightweight tag, just provide a tag name.

git tag v1.9

# Use this to list detailed information that the tag stores.
git show v1.9
Enter fullscreen mode Exit fullscreen mode

List existing git tags.

git tag
Enter fullscreen mode Exit fullscreen mode

This command lists tag in alphabetical order.

# Output
v1.0
v1.1
v1.2
Enter fullscreen mode Exit fullscreen mode

You can also search for tags that match a particular pattern.

git tag -l "v1.8*"
Enter fullscreen mode Exit fullscreen mode
# Output
v1.8
v1.8-rc0
v1.8-rc1
v1.8-rc2
v1.8-rc3
Enter fullscreen mode Exit fullscreen mode

How to delete a git tag?

git tag -d v1.8
# -d = delete
Enter fullscreen mode Exit fullscreen mode

How to move the last tag to the latest commit?

git tag -f -a <tag_name>

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

How to edit a tag message?

git tag -m '<edited_message>' --edit <tag_name> -s -f
Enter fullscreen mode Exit fullscreen mode

How to get the code from a specific tag?

You can checkout a tag and create a new branch from it.

git checkout -b <new_branch_name> <tag_name>
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . .