Categories
git git-clone git-tag

Download a specific tag with Git

2067

I’m trying to figure out how I can download a particular tag of a Git repository – it’s one version behind the current version.

I saw there was a tag for the previous version on the git web page, with object name of something long hex number.

But the version name is “Tagged release 1.1.5” according the site.

I tried a command like this (with names changed):

git clone http://git.abc.net/git/abc.git my_abc

And I did get something – a directory, a bunch of subdirectories, etc.

If it’s the whole repository, how do I get at the version I’m seeking? If not, how do I download that particular version?

4

  • 11

    I develop on a completely different repo then the production, so my production didn’t know any tags when I tried to use git checkout. The solution was to use “git pull –tags” then use git checkout.

    Nov 19, 2011 at 9:35

  • 12

    “git fetch –tags” works too

    – John Erck

    Oct 26, 2012 at 20:37

  • 17

    To avoid cloning the whole repository then switching to a tag, you can directly do a clone -b "Tagged release 1.1.5" http://git.abc.net/git/abs.git my_abc. This will only work if you don’t have a branch with the same name of course (depending on your methodology, this may never happen).

    – RedGlyph

    Oct 5, 2013 at 16:37

  • 3

    @RedGlyph Thanks i will try it. Else we can do like this. git checkout -b new-branch tag-name. Now clone your new-branch. When ever we want we can delete the new-branch.

    – Kalidasan

    Dec 19, 2013 at 7:12


3015

$ git clone

will give you the whole repository.

After the clone, you can list the tags with $ git tag -l and then checkout a specific tag:

$ git checkout tags/<tag_name>

Even better, checkout and create a branch (otherwise you will be on a branch named after the revision number of tag):

$ git checkout tags/<tag_name> -b <branch_name>

18

  • 16

    Yep. git is different to subversion in this respect. A svn tag basically copies the files to a new folder, so you can svn checkout a specific bunch of files, whereas git tags are simply pointers to specific revisions.

    – dbr

    Apr 27, 2009 at 2:17

  • 5

    What if you have a branch and a tag that have the same name? If you just say “git checkout <name>” it says “warning: refname ‘<name>’ is ambiguous. Switched to branch ‘<name>'” — how do you tell it to switch to the tag instead?

    – Tyler

    Nov 24, 2010 at 18:35

  • 56

    when doing a checkout and as Derek mentioned, the repo goes into a “detached head” state. instead, add the -b flag telling git to create a new branch and specify a branch name: git checkout <tag_name> -b <branch_name>

    – hellatan

    May 13, 2012 at 17:42

  • 23

    @hellatan You should only do that when you actually want to create a branch, but most of the time you probably don’t. Running in “detached head” state won’t hurt you, and is likely exactly what you want if you just want to check some git history.

    Jul 1, 2013 at 22:12

  • 4

    In git version 1.8.3.5 and newer, the --branch <tag ref> should allow you to download the repository starting at your <tag ref> as the repo HEAD; combined with --depth 1 will do a shallow tag checkout. See stackoverflow.com/a/21699307/1695680

    Oct 2, 2014 at 20:33

437

git clone --branch my_abc http://git.abc.net/git/abc.git

Will clone the repo and leave you on the tag you are interested in.

Documentation for 1.8.0 of git clone states.

–branch can also take tags and detaches the HEAD at that commit in the resulting repository.

5

  • 9

    This does (at least now) work for tags, though you end up in a detached HEAD state.

    – mxcl

    Jun 24, 2013 at 23:31

  • 80

    FYI: Also specify --depth 1 to avoid downloading any non-current commits.

    – Asclepius

    Mar 27, 2014 at 23:19


  • 5

    This indeed does not work with tags. Only branches. Edit: It looks like only newer versions of git supports that.

    – lzap

    Jul 8, 2014 at 9:05


  • We can also edit .git/config (or somehow configure it) to do a shallow clone of two or more tags, if that might be needed, upgrade a shallow clone to full clone, etc.

    Jan 29, 2015 at 11:41


  • you can also specify the branch you want along with the tag. Like git clone --branch my_abc http://git.abc.net/git/abc.git -b quality quality is the name of the branch we want btw.

    Nov 13, 2019 at 11:19


211

For checking out only a given tag for deployment, I use e.g.:

git clone -b 'v2.0' --single-branch --depth 1 https://github.com/git/git.git

This seems to be the fastest way to check out code from a remote repository if one has only interest in the most recent code instead of in a complete repository. In this way, it resembles the ‘svn co’ command.

Note: Per the Git manual, passing the --depth flag implies --single-branch by default.

–depth

Create a shallow clone with a history truncated to the specified number of commits. Implies –single-branch unless –no-single-branch is given to fetch the histories near the tips of all branches. If you want to clone submodules shallowly, also pass –shallow-submodules.

3

  • 13

    --depth n implies --single-branch. You don’t need both.

    – Niyaz

    Oct 5, 2018 at 1:27

  • 4

    @RyanNerd +1 🙂 Also, just a note for anyone else who spent the last hour figuring this out: do not quote the branch/tag name if running this command on Windows. It will literally include the quotes as-is when looking for the branch/tag

    – Ruslan

    Nov 7, 2018 at 20:29

  • I am trying to clone as part of a script for deployment. Is there a way to hide the ‘detached head’ message? I have tried: git clone -b 'v2.0' --quiet --depth 1 https://github.com/git/git.git That doesn’t work though.

    Nov 14, 2021 at 14:23