[0x04] label management

Keywords: git Ubuntu server

What is a label

When publishing a version, we usually tag it in the version library first, so that we can only determine the version at the time of labeling. Whenever a label version is taken in the future, it is the historical version of the labeled time. Therefore, the tag is also a snapshot of the version library.

Although the Git tag is a snapshot of the version library, it is actually a pointer to a commit (much like a branch, right? But the branch can be moved, but the tag cannot be moved). Therefore, the creation and deletion of tags are completed in an instant.

That is, the branch points to the latest submission, while the label points to a certain submission.

Git has a commit. Why introduce a tag?

Because it's convenient. A large list of commit IDS is annoying and not intuitive enough.
Just like the ip address, who remembers the ip address when there is a domain name?

create label

Tagging in git is very simple. Only 2 steps are required.

  1. Switch to the branch to label.
  2. Execute git tag xxx to label.

In practice, note that the format of tag is v1.0:

root@ubuntu:/home/vth/learngit/merge_test# git tag v1.0
root@ubuntu:/home/vth/learngit/merge_test# 
root@ubuntu:/home/vth/learngit/merge_test# 
root@ubuntu:/home/vth/learngit/merge_test# ls
a.txt  b.txt
root@ubuntu:/home/vth/learngit/merge_test# git branch
  feature1
  feature2
* master
root@ubuntu:/home/vth/learngit/merge_test# git tag
v1.0
root@ubuntu:/home/vth/learngit/merge_test# 

You can also see all the tags using the git tag command.
Note that executing git tag to view the tag has nothing to do with which branch you are currently in. In other words, GIT tag can see the tag typed on all branches.

The default tag is printed on the newly submitted commit. Sometimes, if you forget to label, for example, it's Friday, but the label should be printed on Monday, what should you do?
The method is to find the commit id of the historical submission and mark it:

root@ubuntu:/home/vth/learngit/merge_test# git log --pretty=oneline --abbrev-commit
8765cc7 (HEAD -> feature2) a.txt
da48f02 b.txt
e42bf39 confilict handle
d449421 (feature1) feature1 first commit
845db6c master- second commit
8752952 master - first commit
root@ubuntu:/home/vth/learngit/merge_test# git log --pretty=oneline --abbrev-commit --graph
* 8765cc7 (HEAD -> feature2) a.txt
* da48f02 b.txt
*   e42bf39 confilict handle
|\  
| * d449421 (feature1) feature1 first commit
* | 845db6c master- second commit
|/  
* 8752952 master - first commit
root@ubuntu:/home/vth/learngit/merge_test# git tag v0.1 e42bf39
root@ubuntu:/home/vth/learngit/merge_test# git tag
v0.1
v1.0

Note that the labels are not listed in chronological order, but in alphabetical order.
To view label information:

git show <tagname>

Demonstrate:

root@ubuntu:/home/vth/learngit/merge_test# git show v0.1
commit e42bf39b10555f1b4a028bd792b13ac461ac8629 (tag: v0.1)
Merge: 845db6c d449421
Author: weitonghui <vth1311@126.com>
Date:   Sun Dec 5 21:56:57 2021 +0800

    confilict handle

diff --cc a.txt
index b42e38a,5b0bb2b..3afd506
--- a/a.txt
+++ b/a.txt
@@@ -1,2 -1,1 +1,3 @@@
 +master
 +master1
+ master-feature1

You can also create a label with a description, specify the label name with - A, and specify the description text with - m:

Let's see

root@ubuntu:/home/vth/learngit/merge_test# git tag -a v1.3 -m "1.3woxiachede"
root@ubuntu:/home/vth/learngit/merge_test# git tag
v0.1
v1.0
v1.2
v1.3
root@ubuntu:/home/vth/learngit/merge_test# git show v1.3
tag v1.3
Tagger: weitonghui <vth1311@126.com>
Date:   Sun Dec 5 22:34:33 2021 +0800

1.3woxiachede

commit 8765cc7a2d559fdd5591c67cc54b9f5a6eb4de72 (HEAD -> feature2, tag: v1.3)
Author: weitonghui <vth1311@126.com>
Date:   Sun Dec 5 22:02:27 2021 +0800

    a.txt

diff --git a/a.txt b/a.txt
index 3afd506..d67ce81 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1,4 @@
 master
 master1
 master-feature1
+branch feature2
root@ubuntu:/home/vth/learngit/merge_test# 


Summary

  1. The command git tag < tagName > is used to create a new tag. The default is HEAD. You can also specify a commit id;
  2. The command git tag - a < tagName > - M "BLA..." can specify label information;
  3. The command git tag allows you to view all tags.

Operation label

If the label is wrong, it can also be deleted:

root@ubuntu:/home/vth/learngit/merge_test# git tag
v0.1
v1.0
v1.2
v1.3
root@ubuntu:/home/vth/learngit/merge_test# git tag -d v0.1
Deleted tag 'v0.1' (was e42bf39)

Because the created tags are only stored locally, they will not be automatically pushed to the remote. Therefore, the wrong label can be safely deleted locally.
If you want to push a tag to the remote, use the command git push origin < tagName >:

$ git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
 * [new tag]         v1.0 -> v1.0

Or, push all local tags that have not been pushed to the remote at one time:

$ git push origin --tags
Total 0 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
 * [new tag]         v0.9 -> v0.9

If the tag has been pushed to a remote location, it will be more troublesome to delete the remote tag. First delete it locally:

$ git tag -d v0.9
Deleted tag 'v0.9' (was f52c633)

Then, delete from the remote. The delete command is also push, but the format is as follows:

$ git push origin :refs/tags/v0.9
To github.com:michaelliao/learngit.git
 - [deleted]         v0.9

Summary

The command git push origin < tagName > can push a local label;

The command git push origin --tags can push all local tags that have not been pushed;

The command git tag - d < tagName > can delete a local tag;

The command git push origin: refs / tags / < tagName > can delete a remote Tag (note that deleting a remote tag is a set of combination boxing. You must delete the local tag first and then the remote Tag).

Posted by unklematt on Sun, 05 Dec 2021 15:23:27 -0800