Git tags
If you reach an important milestone and want to remember the commit snapshot forever, you can use git tag to tag it.
Git tags (Tag) are used to mark specific commit points in a repository, typically for releasing versions (such as v1.0, v2.0).
For example, if we want to release a "1.0" version of our runoob project, we can use the git tag -a v1.0 command to tag the latest commit (HEAD) with "v1.0".
The -a option means"create an annotated tag". It can also be executed without the -a option, but it won't record when the tag was made or by whom, nor will it let you add an annotation to the tag. We recommend always creating annotated tags.
Tag syntax format:
git tag
For example:
git tag v1.0
The -a option can add an annotation:
$ git tag -a v1.0
When you run the git tag -a command, Git opens your editor and lets you write a tag annotation, just as you would write an annotation for a commit.
Now, note that when we run git log --decorate, we can see our tags:
* d5e9fc2 (HEAD -> master) Merge branch 'change_site'
|\
| * 7774248 (change_site) changed the runoob.php
* | c68142b Modify code
|/
* c1501a2 removed test.txt、add runoob.php
* 3e92c19 add test.txt
* 3b58100 First version submission
If we forget to tag a commit and have already released it, we can add a tag to it later.
For example, suppose we released commit 85fc7e7 (the last line of the example above) but forgot to tag it at the time. We can now:
$ git tag -a v0.9 85fc7e7
$ git log --oneline --decorate --graph
* d5e9fc2 (HEAD -> master) Merge branch 'change_site'
|\
| * 7774248 (change_site) changed the runoob.php
* | c68142b Modify code
|/
* c1501a2 removed test.txt、add runoob.php
* 3e92c19 add test.txt
* 3b58100 (tag: v0.9) First version commit
If we want to view all tags, we can use the following command:
$ git tag
v0.9
v1.0
Pushing tags to the remote repository
By default, git push does not push tags; you need to push tags explicitly.
git push origin
Push all tags:
git push origin --tags
Deleting a lightweight tag
Delete locally:
git tag -d
Delete remotely:
git push origin --delete
Annotated tags
Annotated tags store the creator's name, email, date, and can contain tag information. Annotated tags are more formal and suitable for scenarios that require extra metadata.
Syntax for creating an annotated tag:
git tag -a -m "message"
For example:
git tag -a -m "runoob.com标签"
PGP signed tag command:
git tag -s -m "runoob.com标签"
View tag information:
git show
Example
The following is a comprehensive example demonstrating how to create, view, push, and delete tags.
Create lightweight and annotated tags:
git tag v1.0
git tag -a v1.1 -m "runoob.com标签"
View tags and tag information:
git tag
git show v1.1
Push tags to the remote repository:
git push origin v1.0
git push origin v1.1
git push origin --tags # 推送所有标签
Delete tags
Delete locally:
git tag -d v1.0
Delete remotely:
git push origin --delete v1.0