627

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?

4
  • 2
    possible duplicate of Download a specific tag with Git Commented Oct 1, 2014 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. :) Commented Oct 8, 2014 at 8: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. Commented Apr 29, 2022 at 8:54
  • There's some good advice in the answers for the similar question linked by @VictorSergienko: stackoverflow.com/questions/791959/… Commented Apr 13, 2023 at 7:13

7 Answers 7

1051
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.

Sign up to request clarification or add additional context in comments.

5 Comments

Note if the ref is ambiguous and you have a branch and a tag named the same thing, this will prefer the branch.
without the optional --depth 1 this is exactly the same as OPs or do i miss something?
@463035818 Seems the same, might be OP did not really have any 2.13.0 tag on remote.
But tags are not branches. How do you get a specific tag?
@Melab, from a man page of git-clone, "--branch can also take tags and detaches the HEAD at that commit in the resulting repository"
155

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 

3 Comments

Is --single-branch equivalent to --depth 1?
No, its not equivalent. --single-branch clones the history for a whole branch. With --depth 1 no history at all is cloned.
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
39
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 

2 Comments

--depth 1 is a gem, so many people download the whole git history just to use the HEAD.
--depth 1 should be made default; if someone tries to chechout a previous commit, they should be prompted to download the rest.
26
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 

2 Comments

The command is character for character exactly the same as the selected answer.
yea but check the example , it's for better uderstading
8

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 

1 Comment

this is working git clone repo_url cd repo git checkout tag_name
3

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.

1 Comment

Cloning a specific tag will detach HEAD. That's the point.
-4

I recommend

git clone --depth 1 [email protected]:etlegacy/etlegacy.git --tags 2.80.2 --single-branch 

4 Comments

Why does this work? I cannot find any documentation on this --tags option. Are you sure that's not just ignored?
Well just try the command and check the results, anyway, on Linux, use auto completion show --tag option
I tried. It doesn't work.
strange, it worked for me last week

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.