Use of Git TAGs

Git can tag versions at a point in time. The function is to mark a point as the version number, such as V1.0.0, V1.0.1, etc., which is often done when releasing a version.

List existing tags

$ git tag //列出仓库内所有的标签

The displayed tags are arranged in alphabetical order, so the order of the tags does not indicate the level of importance.
We can list matching tags with a specific search pattern. In Git's own project repository, there are more than 240 tags. If you are only interested in the 1.4.2 series version, you can run the following command:

$ git tag -l 'v1.4.2.*'
v1.4.2.1
v1.4.2.2
v1.4.2.3
v1.4.2.4

tag

$ git tag -a 1.0.0 -m "Release version 1.0.0"

If you want to add detailed comments, you can omit the "-m" parameter, so that when you execute git tag -a, you will enter the editor to add comments.
The tagging operation happens after we commit the modification to the local repository. Complete example:

$ git add .
$ git commit -m “fixed some bugs”
$ git tag -a 1.0.1 -m “Release version 1.0.1

Submit tags to remote server

$ git push origin master
$ git push origin --tags
–tags参数表示提交所有tag至服务器端,普通的git push origin master操作不会推送标签到服务器端。如果要推送单独的一个标签到服务器,推送的时候直接加上标签的名字:
$ git push origin v1.0.0

You can use the git show command to view the version information of the corresponding tag, together with the commit object when the tag is displayed.

$ git show v1.0.0
tag v1.0.0
Tagger: yuelengloulan <[email protected]>
Date:   Sat May 20 02:13:25 2017 +0800

first tag on master

commit 02059709034621ef9dd329ac024dfe5705cd566d
Author: yuelengloulan <[email protected]>
Date:   Sat May 20 02:06:12 2017 +0800

    first create file

diff --git a/myfile.txt b/myfile.txt
new file mode 100644
index 0000000..29cd67d
--- /dev/null
+++ b/myfile.txt
@@ -0,0 +1 @@
+first my file

We can see that the submitter and submission time of this label are listed above the submission object information, as well as the corresponding label description.

Sync tags from remote server

  • Cloning the repository will synchronize all tags in the repository
$ git clone reponame
  • Pull remote changes
$ git fetch origin --tags 

After executing git fetch and executing git tag, you will see that the remote tag has been pulled to the local.

command to delete tags

  • remove local tags
$ git tag -d v1.0.2
  • Delete the tag of the remote server
$ git push origin :refs/tags/v1.0.2

switch to tab

$ git checkout tagname

After performing the switch, Git prompts to enter the head pointer separation state

Annotated labels

Creating a tag with an annotated type is as simple as specifying the tag name with -a:

$ git tag -a v1.0.2 -m 'Release Version 1.0.2'
$ git tag
v1.0.0
v1.0.1
v1.0.2

The -m option specifies the corresponding label description, which Git will save together in the label object. If this option is not given, Git will launch text editing software for you to enter a tag description.

Lightweight tags

A lightweight tag is actually a file that holds the checksum information of the corresponding commit object. To create such a tag, none of the -a, -s or -m options are needed, just give the tag name:

$ git tag v1.0.3-lw
$ git tag
v1.0.0
v1.0.1
v1.0.2
v1.0.3-lw

Now run git show to see this tag information, and there is only a summary of the corresponding commit object:

$ git show v1.0.3-lw
commit b0d3c14a2b37f89d11ad19b5d02133006c02c0d6
Author: yuelengloulan <yuelengloulan@gmail.com>
Date:   Sat May 20 02:18:05 2017 +0800

    sencond edit commit

diff --git a/myfile.txt b/myfile.txt
index 29cd67d..5075299 100644
--- a/myfile.txt
+++ b/myfile.txt
@@ -1 +1,2 @@
 first my file
+sencond editing

post-fill label

You can even tag an earlier commit at a later stage. For example in the commit history shown below:

$ git log --pretty=oneline
3bbcf2ac3d4cb737cd2f4f86cc55dd0d14d7cdd0 fifth editting
70a49466a460260c372110b785fec577da89468e forth editting
26471a091a03da09976c863e0df384626ac334a6 third editting commit
b0d3c14a2b37f89d11ad19b5d02133006c02c0d6 sencond edit commit
02059709034621ef9dd329ac024dfe5705cd566d first create file

We forgot to version v1.0.4 for this project after committing "forth editting", that's ok, we can do it now. Just keep up with the checksum (or the first few characters) of the corresponding submission object when tagging:

$ git tag -a v1.0.4 70a4

You can see that we have added the label:

$ git tag
v1.0.0
v1.0.1
v1.0.2
v1.0.4
$ git show v1.0.4
tag v1.0.4
Tagger: yuelengloulan <[email protected]>
Date:   Sat May 20 12:18:35 2017 +0800

add tag v1.0.4

commit 70a49466a460260c372110b785fec577da89468e
Author: yuelengloulan <[email protected]>
Date:   Sat May 20 12:12:25 2017 +0800

    forth editting

diff --git a/myfile.txt b/myfile.txt
index df07fdc..fdf3d6a 100644
--- a/myfile.txt
+++ b/myfile.txt
@@ -1,3 +1,4 @@
 first my file
 sencond editing
 third editing
+forth editing

Get the last TAG of a Git project

$ git describe --tags `git rev-list --tags --max-count=1` 

Reference: http://blog.csdn.net/csfreebird/article/details/48903213

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325445060&siteId=291194637