tweeeetyのぶろぐ的めも

アウトプットが少なかったダメな自分をアウトプット<br>\(^o^)/

【git】git tagの使い方をかんたんにおさらい

はじめに

リリース管理やブランチ管理にはまぁまぁ利用するgit tagですがおさらいがてらにメモ

アジェンダ

  1. git tagとは
  2. よく使うgit tagコマンド
  3. tagの作成
  4. tagの一覧
  5. tagの確認
  6. tagの削除
  7. tagのpush
  8. リモートのタグの一覧
  9. リモートのタグの削除

1. git tagとは

タグとは、コミットを参照しやすくするために、わかりやすい名前を付けるものです。

Gitでは、軽量タグと、注釈付きタグの2種類のタグが使用できます。また、一度付けたタグはブランチのように位置が移動することはなく固定です。

説明下手なので参考サイトから引用させていただきました。 * http://www.backlog.jp/git-guide/stepup/stepup4_1.html

2. よく使うgit tagコマンド

あんちょこ的に先にまとめておきます。
コマンドベースでざっとできる事を羅列

# tagの作成
$ git tag タグ
$ git tag -a タグ -m "こめんと"

# tagの一覧
$ git tag

# リモートのタグの一覧
$ git ls-remote --tags

# tagの確認
$ git show タグ

# tagの削除
$ git tag -d タグ

# tagのpush
$ git tag push タグ
$ git push --tags

3. tagの作成

軽量タグの作成

コマンド

git tag タグ

$ git tag v1.0.0

注釈付きタグの作成

コマンド

git tag -a タグ -m ‘コメント’

$ git tag -a v1.0.0 -m 'add readme'

タグをあとからつける

コマンド

git tag -a タグ -m ‘コメント’ コミットハッシュ

$ git tag -a v1.0.0 -m "add readme" 6d0a343aa89b63f6d01116116afcca30c7250ed1

4. tagの一覧

コマンド

git tag

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

5. tagの確認

コマンド

git show タグ

$ git show v1.0.0
tag v1.0.0
Tagger: tweeeety <tweeeety.0719@gmail.com>
Date:   Mon May 8 23:09:11 2017 +0900

add readme

commit 6d0a343aa89b63f6d01116116afcca30c7250ed1
Author: tweeeety <tweeeety.0719@gmail.com>
Date:   Wed Apr 19 21:58:44 2017 +0900

    first commit

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..71d9d7a
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# git-tag-sample

6. tagの削除

コマンド

git tag -d タグ

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

$ git tag -d v1.0.3
Deleted tag 'v1.0.3' (was 8bfcc5a)

$ git tag
v1.0.0
v1.0.1
v1.0.2

7. tagのpush

個別にpush

コマンド

git push origin タグ

$ git push origin v1.0.2
Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 525 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/tweeeety/git-tag-sample.git
 * [new tag]         v1.0.2 -> v1.0.2
補足

タグv1.0.2は一番最後のcommit(8bfcc5aa8eab54d20b46864595de4e8b992c4479)に対してつけたもの。 最初のcommitから最後までpushを一度もしてないままに、
git push origin v1.0.2 とする事もできる。

$ git log
commit 8bfcc5aa8eab54d20b46864595de4e8b992c4479
Author: tweeeety <tweeeety.0719@gmail.com>
Date:   Mon May 8 23:11:22 2017 +0900

    modify third

commit 0715d6e897c84b003ee3f1d9833bfe23d0023355
Author: tweeeety <tweeeety.0719@gmail.com>
Date:   Mon May 8 23:10:42 2017 +0900

    modify second

commit 08c9f32c3f36a7577366440f24b095a697c8645d
Author: tweeeety <tweeeety.0719@gmail.com>
Date:   Mon May 8 23:07:55 2017 +0900

    modify first

commit 6d0a343aa89b63f6d01116116afcca30c7250ed1
Author: tweeeety <tweeeety.0719@gmail.com>
Date:   Wed Apr 19 21:58:44 2017 +0900

    first commit

まとめてpush

それまでpushされてないlocalで作成済みのtagがまとめてpushされます

コマンド

git push –tags

$ git push --tags
Counting objects: 12, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 509 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To https://github.com/tweeeety/git-tag-sample.git
 * [new tag]         v1.0.2 -> v1.0.2
 * [new tag]         v1.0.4 -> v1.0.4
 * [new tag]         v1.0.5 -> v1.0.5

8. リモートのタグの一覧

commitとの紐付けも見れて便利

コマンド

git ls-remote –tags

$ git ls-remote --tags
From https://github.com/tweeeety/git-tag-sample.git
6d0a343aa89b63f6d01116116afcca30c7250ed1  refs/tags/v1.0.0
08c9f32c3f36a7577366440f24b095a697c8645d  refs/tags/v1.0.1
0715d6e897c84b003ee3f1d9833bfe23d0023355  refs/tags/v1.0.2
063adbe5ed48fc19dbea5d92491995e09ad762f2  refs/tags/v1.0.4
33ed691e1c4363b4b5cf7f102fab3ca27f5ba5ab  refs/tags/v1.0.5

9. リモートのタグの削除

コマンド

git push origin :refs/tags/タグ

# リモートのタグを確認
$ git ls-remote --tags
From https://github.com/tweeeety/git-tag-sample.git
6d0a343aa89b63f6d01116116afcca30c7250ed1  refs/tags/v1.0.0
08c9f32c3f36a7577366440f24b095a697c8645d  refs/tags/v1.0.1
0715d6e897c84b003ee3f1d9833bfe23d0023355  refs/tags/v1.0.2
063adbe5ed48fc19dbea5d92491995e09ad762f2  refs/tags/v1.0.4
33ed691e1c4363b4b5cf7f102fab3ca27f5ba5ab  refs/tags/v1.0.5
7bd2f6b05c73cc1042d0b9b8b311e96d3375b13f  refs/tags/v1.0.6

# リモートのタグを削除
$ git push origin :refs/tags/v1.0.6
To https://github.com/tweeeety/git-tag-sample.git
 - [deleted]         v1.0.6

# 消えてるか確認
$ git ls-remote --tags
From https://github.com/tweeeety/git-tag-sample.git
6d0a343aa89b63f6d01116116afcca30c7250ed1  refs/tags/v1.0.0
08c9f32c3f36a7577366440f24b095a697c8645d  refs/tags/v1.0.1
0715d6e897c84b003ee3f1d9833bfe23d0023355  refs/tags/v1.0.2
063adbe5ed48fc19dbea5d92491995e09ad762f2  refs/tags/v1.0.4
33ed691e1c4363b4b5cf7f102fab3ca27f5ba5ab  refs/tags/v1.0.5

# ローカルには残ってるので別途消してね
$ git tag
v1.0.0
v1.0.1
v1.0.2
v1.0.4
v1.0.5
v1.0.6

リポジトリ

https://github.com/tweeeety/git-tag-sample

終わり

おさらいというかあんちょこ的になったけど、それはそれで良し\(^o^)/