作業ノート

様々なまとめ、雑感など

gitで最後に付けられたタグを確認する

git describeコマンドを使って、対象のブランチの最新のタグ名を確認できる。

例えば

# 0.リポジトリ作成
$ git init

# 1.ファイルを1つ追加
$ touch tag01.txt
$ git commit -m 'add'
[master (root-commit) aede06b] add
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 tag01.txt

# 2.注釈付きタグを打つ
$ git tag -a tag01 -m 'tag01'

# 3.ファイルを1つ追加
$ touch tag02.txt
$ git add tag02.txt
$ git commit -m 'add tag02.txt'
[master 47b9490] add tag02.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 tag02.txt

このときにgit describeコマンドを実行すると

$ git describe
tag01-1-g47b9490

このような結果になる。

ハイフンで区切られた各項目は、それぞれのルールに従って所定の値が付与される。

  • tag01: そのコミットからさかのぼって最初に見つかるタグ名。
  • 1: tag01のタグ以降、当該コミットが何番目のコミットになるか。
  • g47b9490: 先頭のgは、gitの略称。2文字目以降は当該コミットのハッシュ値。

最後のハッシュ値は、--abbrevオプションで表示文字数を変えることができる。特に0を指定した場合、先頭のタグ名のみが表示される。

$ git describe --abbrev=0
tag01

ちなみに、ハッシュ値の最小表示文字数は4文字であり、それ未満の値を指定しても4文字表示される。

$ git describe --abbrev=1
tag01-1-g47b9

git describeコマンドが対象とするタグは、注釈付き(annotated)タグのみ。 対象に軽量(lightweight)タグを加えたい場合は、上述のコマンドに--tagsオプションを追加する。

# 4.ファイルを追加
$ touch tag03.txt
$ git add tag03.txt
$ git commit -m 'add tag03.txt'
[master 9a0c201] add tag03.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 tag03.txt

# 5.軽量タグをつける
$ git tag tag02

# tagsオプションなし -> 2の注釈付きタグを直近のタグとみなす
$ git describe
tag01-2-g9a0c201

# tagsオプションあり -> 5の軽量タグを直近のタグとみなす
$ git describe --tags
tag02

参考