tags:

views:

58

answers:

4

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.

A: 

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.

VonC
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
@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
A: 

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

mb14
Well, git-checkout also calls it a branch, but works on tags so I think the question is valid.
grm
@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
AFAIK: There is no new branch crated in any of the examples. It's just a reference.
grm
@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
A: 

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
I'm looking into doing deployment directly from the tag system.
grm
A: 

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
I will definitively look into that. The overhead of cloning is probably not worth it. Thanks.
grm