Git標籤簡介

Git可以對某一時間點上的版本打上標籤。人們在發佈某個軟件版本(比如 v1.0 等等)的時候,經常這麼做。本節我們一起來學習如何列出所有可用的標籤,如何新建標籤,以及各種不同類型標籤之間的差別。

新建標籤

假如說,我們某個應用經過一段時間的開發,完成了某個功能,現在需要往線上發佈。我們可以先將開發代碼合併到master,然後對當前的master打一個標籤,來標識當前的發佈版本。假如說就叫v1.0:

將當前代碼完成提交

yanwei@ubuntu:~/git_test$ git add *
yanwei@ubuntu:~/git_test$ git commit -m "v1.0最後一次提交"
[master c169872] v1.0最後一次提交
 2 files changed, 1 insertion(+)
 create mode 100644 new.txt
yanwei@ubuntu:~/git_test$ git status

位於分支 master

無文件要提交,乾淨的工作區

創建一個v1.0的tag

yanwei@ubuntu:~/git_test$ git tag v1.0

查看所有的tag

yanwei@ubuntu:~/git_test$ git tag
v1.0

還可以通過如下方式查看指定的標籤:

yanwei@ubuntu:~/git_test$ git tag -l "v1.*"
v1.0

在上面的示例中,我們看到有了一個v1.0的tag,可是這個tag沒有任何的描述信息,也不知道具體是幹嘛的。這個時候,我們可以在打tag的時候, 添加一些詳細的信息:

-a指定標籤爲含註釋的標籤,-m指定註釋

yanwei@ubuntu:~/git_test$ git tag -a v1.1 -m "v1.1版本,啥都沒改"

查看標籤詳細信息

如果要查看一個標籤的詳細信息,可以使用如下方式:

yanwei@ubuntu:~/git_test$ git show v1.1
tag v1.1
Tagger: yanwei 
Date:   Mon Jul 16 19:07:24 2018 +0800

v1.1版本,啥都沒改

commit c16987225db5f8ff65c7ff858eff4a75992f61dd (HEAD -> master, tag: v1.1, tag: v1.0)
Author: yanwei 
Date:   Mon Jul 16 19:01:51 2018 +0800

v1.0最後一次提交

diff --git a/code.txt b/code.txt
index e064e4c..8f8a0e7 100644
--- a/code.txt
+++ b/code.txt
@@ -5,3 +5,4 @@ this is the forth line
 this is the master branch
 this is dev branch
 this is dev branch new line
+this line for bug
diff --git a/new.txt b/new.txt
new file mode 100644
index 0000000..e69de29

切換標籤

切換標籤的操作與切換分支的命令相同:

git checkout [tagname]

後期添加標籤

在一些應用場景中,我們一個版本發佈之後,並沒有爲其添加標籤,後期爲了規範化管理,回過頭來,想對那些版本添加標籤,可以使用如下的操作方式:

使用git log列出一些歷史版本信息:

yanwei@ubuntu:~/git_test$ git log --oneline
c169872 (HEAD -> master, tag: v1.1, tag: v1.0) v1.0最後一次提交
d69c612 合併bug分支
67de5f6 修復bug
ea9a7d5 merge with no-ff
b89266d (dev) dev branch another commit
6c1828d 解決衝突
2015000 master new commit
187dee6 dev first commit
0a96a0f forth commit
e4fb2aa third commit
227ecaa second commit
d66bdc0 first commit

指定爲dev first commit這個版本打一個標籤:

git tag -a v1.2 187dee6

將標籤推送到遠端倉庫

默認情況下,git push 並不會把標籤傳送到遠端服務器上,只有通過顯式命令才能分享標籤到遠端倉庫。其命令格式如同推送分支,運行git push origin [tagname] 即可:

git push origin v1.1

本文地址:https://www.linuxprobe.com/git-tags.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章