This command fetches all tags:
git fetch origin --tags
This command fetches a specific tag:
git fetch origin refs/tags/1.0.0
But that doesn't let me do:
git checkout tags/2.3.18
How can I fetch a single tag and then perform a checkout?
This fails because it doesn't write a local reference: it obtains the remote's
refs/tags/1.0.0
, and any tag object(s), commits, etc., required to go with it; it drops those intoFETCH_HEAD
(as allgit fetch
commands always do); and ... that's it. It never creates referencerefs/tags/1.0.0
in your repository, even though it got everything it needed to do so.To make it create such a tag if it does not yet exist:
The name on the right of the colon is the name your Git will use in your repository. You could turn this tag into a branch named
wacky
, for instance, by naming itrefs/heads/wacky
. (There's no reason to do this. I am describing this just for illustration.)This is a non-forced fetch, so if you already have a
refs/tags/1.0.0
, your Git will refuse to update your reference. If you wish to overwrite any existing1.0.0
tag, use:If you wish to fetch all tags, with or without overwriting:
with or without a leading plus sign. (Note: the quote marks are just to protect the
*
from your shell. The plus sign may go inside or outside the quotes. In fact, the quotes themselves can go anywhere as long as they surround all asterisks or other shell meta-characters:or you can use backslashes instead:
In all cases we are just protecting the sensitive asterisk from the shell's "asterisks are tasty, let's eat them" functions.)
I read all the answers, but there is not yet mentioned one syntactic sugar. If you need to fetch only one tag as a tag (to checkout later) you can write, for instance for
refs/tags/2.3.18
:This is shortcut for already mentioned:
Of course, you may not use
--no-tags
if you need other tags (according to the default behavior) or if you already explicitly set--no-tag
in theclone
command or intagOpt
config option(man git-clone
).When you specify explicit refs to
git fetch
, you need to either provide a mapping in your refspec or do what you want with the fetched id's recorded inFETCH_HEAD
yourself.The advantage to having the no-mapping-provided case do no default processing on the fetched refs is, the no-mapping-provided case does no default processing on the fetched refs. You might appreciate
when you really only want just the one tag set up in your local repo and not however many dozens of prior tags in the origin repo point into the fetched history.