tag標簽

打標簽

Git 可以給歷史中的某一個提交打上標簽,以示重要。 比
較有代表性的是人們會使用這個功能來標記發布結點(v1.0 等等)

列出標簽

git tag 列出所有標簽
git tag -l 'v1.8*' 列出標簽前面字符是v1.8的所有標簽

$git tag -l  'v2.9*'
v2.9.0
v2.9.0-rc0
v2.9.0-rc1
v2.9.0-rc2
v2.9.1
v2.9.2
v2.9.3
v2.9.4
v2.9.5

創建標簽

默認標簽創建在最新提交的commit上

  • 輕量級標簽 特定提交的一個引用
  • 附注標簽 是存儲在git庫的一個完整對象,包括打標簽者的姓名、郵箱、創建時間等(建議創建附注標簽)

輕量級標簽

# 創建
# 輕量級標簽,不需要提供-a、-m、-s等參數,直接提供標簽名
$ git tag test_lightweight

附注標簽

# 創建
$ git tag -a v1.0 -m 'test tag v1.0'
# -a 添加附注標簽
# -m 編寫標簽信息

-m 如果沒有提供,git會運行編輯器要求你輸入信息

追加(后期)打標簽

# 查看提交紀錄
$ git log --pretty=oneline
83428cee2f039c2cfd3a2cc93e952c2273c56e6f (HEAD -> master, origin/master, origin/HEAD) add e
a03793cc16833f2347538ddb455e6f273a026d62 add d
c909fd331b2dd74f4a031eb935b3ed4d7655bdf6 (tag: v1.2) add c

現在在add d上增加標簽

$ git tag -a v1.3 a03793cc16833 -m 'add tag'
$ git log --pretty=oneline
83428cee2f039c2cfd3a2cc93e952c2273c56e6f (HEAD -> master, origin/master, origin/HEAD) add e
a03793cc16833f2347538ddb455e6f273a026d62 (tag: v1.3) add d
c909fd331b2dd74f4a031eb935b3ed4d7655bdf6 (tag: v1.2) add c

查看標簽

git show tag_name

# 附注標簽的 tag show
$git show v1.1
tag v1.1
Tagger: yin <yjd48676@ly.com>
Date:   Sat Dec 16 14:14:10 2017 +0800

test tag

commit 439ac73c29a0fe1c10fd975dc48f766e54e20654 (tag: v1.1)
Author: yin <yjd48676@ly.com>
Date:   Sat Dec 16 14:13:17 2017 +0800

    add b


# 輕量級的 tag show
$git show  test_lightweight
commit d81250633475814521e66e78b102a501f3cf2ebe (tag: test_lightweight, tag: show)
Author: yin <yjd48676@ly.com>
Date:   Sat Dec 16 14:07:25 2017 +0800

    add a

共享標簽(推送標簽到遠端服務器)

默認情況下git push并不會推送標簽到遠端服務器,在創建完標簽后需要顯示的推送標簽到服務器上。
推一個標簽git push origin [tagname]
推所有標簽git push origin --tags

# 推一個標簽
$ git push origin v1.3
Counting objects: 1, done.
Writing objects: 100% (1/1), 150 bytes | 150.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To github.com:JinduYin/test_git.git
 * [new tag]         v1.3 -> v1.3

# 推所有標簽 同步本地的所有tag到服務器
$ git push origin --tags
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 272 bytes | 272.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:JinduYin/test_git.git
 * [new tag]         show -> show
 * [new tag]         test_lightweight -> test_lightweight
 * [new tag]         v1.1 -> v1.1
 * [new tag]         v1.2 -> v1.2

獲取指定tag代碼

git checkout tag_name 切換到某個tag

$ git checkout v1.2
Note: checking out 'v1.2'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at c909fd3... add c

"detached HEAD" 狀態
如果你想編輯此tag下的代碼,上面的方法就不適用了
需要把tag的快照對應的代碼拉取到一個新分支上

git checkout -b dev v1.2 在tag v1.2處新建分支

$ git checkout -b dev v1.2
Switched to a new branch 'dev'
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容