Mac中Git的簡單實用(8) --- 標籤使用

今天我來介紹下Git,Git是一款免費、開源的分佈式版本控制系統。
我們在上一個學習了Git的遠程庫的信息、推送分支、pull分支。
這一章,我們要學習標籤的使用。

Mac中Git的簡單實用(1) — Git基本命令(1)
Mac中Git的簡單實用(2) — Git基本命令(2)
Mac中Git的簡單實用(3) — Github遠程倉庫
Mac中Git的簡單實用(4) — 分支branch管理
Mac中Git的簡單實用(5) — Git分支衝突管理
Mac中Git的簡單實用(6) — 分支管理策略
Mac中Git的簡單實用(7) — 多人協作使用


1、標籤簡介

我們通常先在發佈應用時候先在版本庫中打一個標籤,就唯一確定了打標籤時刻的版本。
將來無論什麼時候,取某個標籤的版本,就是把那個打標籤的時刻的歷史版本取出來。
所以,標籤也是版本庫的一個快照。
Git的標籤雖然是版本庫的快照,但其實它就是指向某個commit的指針,所以,創建和刪除標籤都是瞬間完成的。

2、創建標籤

在Git中打標籤非常簡單,首先,切換到需要打標籤的分支上:

    MBP:git qiuyu$ git branch
    * dev
      master
    MBP:git qiuyu$ git checkout master
    A   test.c
    切換到分支 'master'
    您的分支與上游分支 'origin/master' 一致。
    MBP:git qiuyu$ 

然後,敲命令git tag 就可以打一個新標籤:

    MBP:git qiuyu$ git tag v1.0

可以用命令git tag查看所有標籤:

    MBP:git qiuyu$ git tag
    v1.0

默認標籤是打在最新提交上的。
如果需要在歷史版本上面添加標籤,方法是找到歷史提交的commit id,然後打上就可以了。
通過git log –pretty=oneline –abbrev-commit命令查看:

    MBP:git qiuyu$ git log --pretty=oneline --abbrev-commit
    4df3131 the forth release
    b5795fe debug01
    cfe4639 debug01
    03d3111 merge with no-ff
    d60705f no fast-forward
    33605db the lateset
    89519de master
    b00c593 branch1
    d2ba8f0 branch test
    e5798f6 the third release
    f49806e the second release
    4644630 the first release

比方說要對merge with no-ff這次提交打標籤,它對應的commit id是03d3111,敲入命令:

    MBP:git qiuyu$ git tag v1.1 03d3111
    MBP:git qiuyu$ git tag
    v1.0
    v1.1

注意,標籤不是按時間順序列出,而是按字母排序的。可以用git show 查看標籤信息:

    MBP:git qiuyu$ git show v1.1
    commit 03d3111fa727da8660cedf0cdc721115147ca0c3
    Merge: 33605db d60705f
    Author: qiuyu <[email protected]>
    Date:   Fri Jun 19 19:02:16 2015 +0800
        merge with no-ff

還可以創建帶有說明的標籤,用-a指定標籤名,-m指定說明文字;
git tag -a -m “blablabla…”可以指定標籤信息:

    MBP:git qiuyu$ git tag -a v1.2 -m "version 1.2 release" cfe4639
    MBP:git qiuyu$ git show v1.2
    tag v1.2
    Tagger: qiuyu <[email protected]>
    Date:   Sat Jun 27 17:22:39 2015 +0800

    version 1.2 release

    commit cfe46396593f86edabdadfb55ee2a0b0a47670f5
    Author: qiuyu <[email protected]>
    Date:   Fri Jun 19 19:51:21 2015 +0800

        debug01

3、操作標籤

刪除標籤操作:

MBP:git qiuyu$ git tag -d v1.2
已刪除 tag 'v1.2'(曾爲 1fdf6da)
MBP:git qiuyu$ git tag
v1.0
v1.1

因爲創建的標籤都只存儲在本地。所以,打錯的標籤可以在本地安全刪除。

如果要推送某個標籤到遠程,使用命令git push origin :

MBP:git qiuyu$ git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:QyMars/git.git
 * [new tag]         v1.0 -> v1.0 

一次性推送全部尚未推送到遠程的本地標籤:

MBP:git qiuyu$ git push origin --tags
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:QyMars/git.git
 * [new tag]         v1.1 -> v1.1 

如果標籤已經推送到遠程,要刪除遠程標籤就麻煩一點,先從本地刪除;
然後,從遠程刪除。刪除命令也是push,但是格式如下:

MBP:git qiuyu$ git tag -d v1.1
已刪除 tag 'v1.1'(曾爲 03d3111)
MBP:git qiuyu$ git push origin :refs/tags/v1.1
To [email protected]:QyMars/git.git
 - [deleted]         v1.1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章