6

I'm a bit puzzled why I'm unable to checkout a tag directly from the git clone command. What I try to do is:

git clone -b mytag <url>/foo.git

The error I get is:

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

Tag is present and e.g.

cd foo && git checkout mytag 

...works well.

If anyone could share some light on why it's impossible to clone directly into a tag I would appreciate it. Thank you.

grm
  • 5,197
  • 5
  • 29
  • 35
  • So, is your question : why that doesn't work ? or What is the best workaround ? – mb14 Aug 09 '10 at 09:36
  • I guess both questions has been explained here now, although I find the inconsistency a bit confusing. I will keep it up until tomorrow to see if there is some other approaches, then I will close it. Thanks for explanation @mb14, @simplyharsh, @VonC ! – grm Aug 09 '10 at 13:50

4 Answers4

4

Perhaps all you really need/want to do is use git archive to pull a tarball of anything git rev-parse can understand. You can use the --remote option to pull the archive from some remote source identically to the <url>/foo.git value that you are passing to clone. In theory, this will be much quicker since all you will be grabbing is the working tree and not the entire repository.

Here's a "works for me" example:

% git archive --prefix foo/ --remote <url>/foo.git my-tag | tar -xf -
Brian Phillips
  • 12,693
  • 3
  • 29
  • 26
  • I will definitively look into that. The overhead of cloning is probably not worth it. Thanks. – grm Aug 09 '10 at 16:45
0

Are you in a detached head after the git checkout mytag?

It is possible that mytag is not in the "refs/heads/" namespace (for branche HEADs) of foo.git, but still reference a valid commit.

That would explain the warning in git clone, while the git checkout runs ok.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yes, I'm in a detached head. I'm not sure I understand the difference between git checkout and git clone in this case. Would an annotated tag help? – grm Aug 09 '10 at 09:54
  • @grm: no it won't make any difference. the git clone -b is for branch. Any other refspec might be accessible though a `git checkout`, but you will be in a DETACHED HEAD (meaning you will need to create a branch) – VonC Aug 09 '10 at 10:15
0

-b is meant to checkout a branch not a tag. That's why it's saying remote BRANCH not found ;-)

mb14
  • 22,276
  • 7
  • 60
  • 102
  • Well, git-checkout also calls it a branch, but works on tags so I think the question is valid. – grm Aug 09 '10 at 08:51
  • @grm I m not saying the question is invalid, but just answering the question. Maybe they have implemented it for checkout but not for clone. BTW git checkout -b on tag create a new branch, I'm not really suprised that you can't create a new branch before having finishing cloning the repo. – mb14 Aug 09 '10 at 09:15
  • AFAIK: There is no new branch crated in any of the examples. It's just a reference. – grm Aug 09 '10 at 09:56
  • @grm, my mistake git checkout -b tagname , doesn't checkout tagname but just create a new branch with the name : tagname. So what do you mean by 'git checkout also calls it a branch' ? you can pass any commit to git checkout, but the -b in git-clone and git-checkout have a different meaning (but always refer to a branch, never a tag) – mb14 Aug 09 '10 at 10:14
0

Cloning directly into a tag, beats me intention-wise.

But the command you are using, actually is used to clone repository's branch. So you are actually asking to checkout branch name mytag from repository. mytag branch is obviously not in your remote repository.

And I don't think there is any direct way to clone into a new tag. You have to clone and then apply tags or fetch the tags of remote repository explicitly using git fetch --tags $URL.

Edit on response:

Well, deployment from tag is a common usage style. As code is usually tagged once its at a considerable state. You can checkout mytag once you cloned and fetched tags from remote repositories.

git checkout mytag

or even

git checkout -b mytagbranch mytag

and continue the deployment.

simplyharsh
  • 35,488
  • 12
  • 65
  • 73