496

From git-clone(1) Manual Page

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

I tried

git clone --branch <tag_name> <repo_url>

But it does not work. It returns:

warning: Remote branch 2.13.0 not found in upstream origin, using HEAD instead

How to use this parameter?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Jiang Jun
  • 5,245
  • 3
  • 16
  • 13
  • 1
    possible duplicate of [Download a specific tag with Git](http://stackoverflow.com/questions/791959/download-a-specific-tag-with-git) – Victor Sergienko Oct 01 '14 at 16:30
  • 5
    You're right, but little difference. When I ask this question, in my situation, I needed to do this in one line and must use `clone`, and I was stuck at 'why --branch doesn't work'. The best answer of that url used `clone`->`checkout`, which cannot resolve my question. :) – Jiang Jun Oct 08 '14 at 08:35
  • 3
    Not sure if the problem persists after five months, but most likely, the tag is not *2.13.0* but *v2.13.0*. Been there, done that. – berndbausch Apr 29 '22 at 08:54
  • There's some good advice in the answers for the similar question linked by @VictorSergienko: https://stackoverflow.com/questions/791959/download-a-specific-tag-with-git – NeilG Apr 13 '23 at 07:13

7 Answers7

824
git clone --depth 1 --branch <tag_name> <repo_url>

--depth 1 is optional but if you only need the state at that one revision, you probably want to skip downloading all the history up to that revision.

Giszmo
  • 1,944
  • 2
  • 20
  • 47
Erik Saunier
  • 8,670
  • 1
  • 20
  • 15
124

Use --single-branch option to only clone history leading to tip of the tag. This saves a lot of unnecessary code from being cloned.

git clone <repo_url> --branch <tag_name> --single-branch
GGO
  • 2,678
  • 4
  • 20
  • 42
Sahil kalra
  • 8,344
  • 4
  • 23
  • 29
  • 14
    Is `--single-branch` equivalent to `--depth 1`? – igracia Jan 14 '16 at 14:34
  • 44
    No, its not equivalent. --single-branch clones the history for a whole branch. With --depth 1 no history at all is cloned. – Martin Krung Feb 02 '16 at 11:15
  • 18
    Also `--single-branch` is implied when `--depth` is used. From the manual `When creating a shallow clone with the --depth option, this is the default` – koda Nov 18 '19 at 15:51
36
git clone -b 13.1rc1-Gotham  --depth 1  https://github.com/xbmc/xbmc.git
Cloning into 'xbmc'...
remote: Counting objects: 17977, done.
remote: Compressing objects: 100% (13473/13473), done.
Receiving objects:  36% (6554/17977), 19.21 MiB | 469 KiB/s    

Will be faster than :

git clone https://github.com/xbmc/xbmc.git
Cloning into 'xbmc'...
remote: Reusing existing pack: 281705, done.
remote: Counting objects: 533, done.
remote: Compressing objects: 100% (177/177), done.
Receiving objects:  14% (40643/282238), 55.46 MiB | 578 KiB/s

Or

git clone -b 13.1rc1-Gotham  https://github.com/xbmc/xbmc.git
Cloning into 'xbmc'...
remote: Reusing existing pack: 281705, done.
remote: Counting objects: 533, done.
remote: Compressing objects: 100% (177/177), done.
Receiving objects:  12% (34441/282238), 20.25 MiB | 461 KiB/s
RzR
  • 3,068
  • 29
  • 26
  • 13
    `--depth 1` is a gem, so many people download the whole git history just to use the `HEAD`. – MGP Nov 28 '14 at 15:17
  • 8
    `--depth 1` should be made default; if someone tries to chechout a previous commit, they should be prompted to download the rest. – Jikku Jose Jun 30 '16 at 15:39
15
git clone --depth 1 --branch <tag_name> <repo_url>

Example

git clone --depth 1 --branch 0.37.2 https://github.com/apache/incubator-superset.git

<tag_name> : 0.37.2

<repo_url> : https://github.com/apache/incubator-superset.git
Akitha_MJ
  • 3,882
  • 25
  • 20
7

Use the command

git clone --help

to see whether your git supports the command

git clone --branch tag_name

If not, just do the following:

git clone repo_url 
cd repo
git checkout tag_name
mathsyouth
  • 3,770
  • 1
  • 20
  • 18
1

Cloning a specific tag, might return 'detached HEAD' state.

As a workaround, try to clone the repo first, and then checkout a specific tag. For example:

repo_url=https://github.com/owner/project.git
repo_dir=$(basename $repo_url .git)
repo_tag=0.5

git clone --single-branch $repo_url # using --depth 1 can show no tags
git --work-tree=$repo_dir --git-dir=$repo_dir/.git checkout tags/$repo_tag

Note: Since Git 1.8.5, you can use -C <path>, instead of --work-tree and --git-dir.

Noam Manos
  • 15,216
  • 3
  • 86
  • 85
-2

I recommend

git clone --depth 1 git@github.com:etlegacy/etlegacy.git --tags 2.80.2 --single-branch 
Yunnosch
  • 26,130
  • 9
  • 42
  • 54