試した環境
本題
git でリモートリポジトリからタグを1つだけ取得するには以下のようなコマンドで行います。
git fetch --no-tags <remote_name> tag <tag_name>
ポイントはタグ名の前にtag
と付けることと、--no-tags
オプションを付けることです。
例えばタグ名がv2.0
だとして、git fetch --no-tags origin tag v2.0
と実行すると以下のように出力されます。
From https://.... * [new tag] v2.0 -> v2.0
tagについて
tag
がないとローカルにタグとして取得できません。
例えばタグ名がv3.0
だとして、git fetch --no-tags origin v3.0
と実行すると以下のように出力されます。
From https://.... * tag v3.0 -> FETCH_HEAD
この後git log
などで確認してもローカルリポジトリにタグが付いていないことが確認できると思います。
git-fetch のドキュメントに以下の記述があります。
tag <tag>
means the same asrefs/tags/<tag>:refs/tags/<tag>
; it requests fetching everything up to the given tag.
なので以下のコマンドも同様にローカルリポジトリにタグを取得できます。
git fetch --no-tags <remote_name> refs/tags/<tag_name>:refs/tags/<tag_name> git fetch --no-tags <remote_name> <tag_name>:refs/tags/<tag_name>
--no-tags
オプションについて
--no-tags
オプションがないと、すべてのタグが取得されます。
例えばタグ名がv4.0
だとして、git fetch origin tag v4.0
と実行すると以下のように出力されます。
From https://.... * [new tag] v4.0 -> v4.0 * [new tag] v1.0 -> v1.0 * [new tag] v2.0 -> v2.0 * [new tag] v3.0 -> v3.0
git config remote.<name>.tagOpt
が--no-tags
に設定されている場合はコマンドに--no-tags
オプションは不要です。
例えばgit clone --no-tags <URL>
などでクローンしたローカルリポジトリでは初めからそのような設定になります。
参考
https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt-ltrefspecgt
https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt---no-tags