Git Tags

If you reach an important stage and want to always remember the snapshot of a commit, you can usegit tagTag it.

Git tags are used to mark specific commit points in a repository, typically for release versions (such as v1.0, v2.0).

For example, if we want to release a "1.0" version for our example project, we can usegit tag -a v1.0The command tags the latest commit (HEAD) with "v1.0".

-aOptions意is

"Create an annotated tag", not needed-aThis option also works, but it won't record when the tag was created, by whom, or let you add an annotation to the tag. We recommend always creating annotated tags.

Tag syntax format:

git tag <tagname>

For example:

git tag v1.0

-aThe option can add an annotation:

$ git tag -a v1.0 

When you run the git tag -a command, Git will open your editor and let you write a tag annotation, just like writing an annotation for a commit.

Now, note that when we executegit log --decorateAt this point, we can see our tag:

*   d5e9fc2 (HEAD -> master) Merge branch 'change_site'
|\  
| * 7774248 (change_site) changed the example.php
* | c68142b 修改代码
|/  
* c1501a2 removed test.txt、add example.php
* 3e92c19 add test.txt
* 3b58100 第一次版本提交

If we forgot to tag a commit and already released it, we can add a tag to it afterwards.

For example, suppose we released commit 85fc7e7 (the last line in the example above), but forgot to tag it at that time. We can now also:

$ git tag -a v0.9 85fc7e7
$ git log --oneline --decorate --graph
*   d5e9fc2 (HEAD -> master) Merge branch 'change_site'
|\  
| * 7774248 (change_site) changed the example.php
* | c68142b 修改代码
|/  
* c1501a2 removed test.txt、add example.php
* 3e92c19 add test.txt
* 3b58100 (tag: v0.9) 第一次版本提交

If we want to view all tags, we can use the following command:

$ git tag
v0.9
v1.0

Push tags to the remote repository

By default, git push does not push tags; you need to explicitly push tags.

git push origin <tagname>

Push all tags:

git push origin --tags

Delete lightweight tag

Delete locally:

git tag -d <tagname>

Delete remotely:

git push origin --delete <tagname>

附NoteTags

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 additional metadata.

Syntax for creating annotated tags:

git tag -a <tagname> -m "message"
For example:
git tag -a <tagname> -m "example.com标签"

PGP-signed tag command:

git tag -s <tagname> -m "example.com标签"

View tag information:

git show <tagname>

Example

The following is a comprehensive example demonstrating how to create, view, push, and delete tags.

Creating lightweight tags and annotated tags:

git tag v1.0
git tag -a v1.1 -m "example.com标签"

Viewing 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 tag

Delete locally:

git tag -d v1.0

Delete remotely:

git push origin --delete v1.0
other extensions