Git tags are a way to mark a specific point in Git history. They are typically used to mark release versions, significant changes, or important milestones in a project. There are two types of tags in Git: lightweight tags
and annotated tags
.
Lightweight tags are simple tags that point directly to a specific commit. They are easy to create and do not contain any additional information besides the tag name and the commit it points to.
To create a lightweight tag, use the following command:
git tag "tag name"
For example, if you want to tag the latest commit in your project with the tag name v1.0
, you would run the following command:
git tag v1.0
Annotated tags are tags that contain additional information, such as a tagger name, email address, and a message describing the purpose of the tag. Annotated tags are stored as full Git objects in the Git repository, making them more reliable and easier to manage.
To create an annotated tag, use the following command:
git tag --annotate "tag name"
or
git tag -a "tag name"
For example, if you want to tag the latest commit in your project with the tag name v1.0
and a message describing the purpose of the tag, you would run the following command:
git tag --annotate "v1.0" -m "Release version 1.0"
You can also add an annotated tag to a specific commit by specifying the commit’s SHA hash:
git tag --annotate "tag name" <commit-sha>
For example, if you want to add an annotated tag to a specific commit with the SHA hash abc123
and the tag name v1.0
, you would run the following command:
git tag --annotate "v1.0" abc123 -m "Release version 1.0"
To share your tags with others, you need to push them to a remote repository, such as the origin
repository. You can push a single tag to the origin
repository using the following command:
git push origin "tag name"
For example, if you want to push the v1.0
tag to the origin repository, you would run the following command:
git push origin v1.0
You can also push all of your local tags to the origin repository at once using the following command:
git push origin --tags
To view a list of all the tags in your Git repository, use the following command:
git tag --list
or
git tag
Example:
$ git tag
v1.0
v1.1
v1.2
To view a list of all the remote tags in a Git repository, use the following command:
$ git ls-remote --tags origin
Example:
$ git ls-remote --tags origin
de3fa3dccfa6a08fa1127e82e1747f9a57f9c9d6 refs/tags/v1.0
65ba4e6ac71ac6c3a16a84a9c74d1b078f44c5d2 refs/tags/v2.0
You may need to delete a tag if you made a mistake while creating it or if you no longer need it. Deleting a tag is a straightforward process.
To delete a tag, use the following command:
$ git tag -d "tag name"
Example:
$ git tag -d v2.0
Deleted tag 'v2.0' (was 65ba4e6)
Displaying tag information is useful when you want to see the details of a particular tag. For example, you can see the commit message, author, and date, as well as the changes made in the file.
To view information about a particular tag, use the following command:
$ git show "tag name"
For example:
$ git show v3.0
commit 9e874b8af9270824e6784eebb79cc3bc3f863fa8
Author: XAbirHasan <abir@example.com>
Date: Tue Jan 10 14:00:00 2023 -0800
Fixed a bug in the code
diff --git a/file.txt b/file.txt
index e69de29..1b2e1d63 100644
--- a/file.txt
+++ b/file.txt
@@ -0,0 +1 @@
+This is a new line in the file
Listing tags by date is useful when you want to see the tags in chronological order. This can help you understand the development timeline and keep track of changes made to the code over time.
To list tags in order of creation date, use the following command:
$ git for-each-ref --sort=taggerdate refs/tags
For example:
$ git for-each-ref --sort=taggerdate refs/tags
9e874b8af9270824e6784eebb79cc3bc3f863fa8 tag: v3.0, tagger: XAbirHasan <abir@example.com>, Wed Jan 11 14:30:00 2023 -0800
65ba4e6ca43c298250cee96991df9d65db38c2f7 tag: v2.0, tagger: XAbirHasan <abir@example.com>, Tue Jan 10 14:00:00 2023 -0800
Merging tags is useful when you have multiple tags that represent the same version of the code and you want to combine them into a single tag. This can help you keep your Git repository organized and ensure that you have a clear and concise history of your code changes.
To merge two or more tags into a single tag, use the following command:
$ git tag -a "new tag name" "existing tag name1" "existing tag name2"
For example:
$ git tag -a v3.5 v3.0 v3.1
Git tags are an important tool for managing and tracking code changes in a Git repository. By using tags, you can keep track of different versions of the code, manage releases, and keep track of development progress. The commands for creating, deleting, and managing tags in Git are straightforward and easy to use.