threads / discuss / 27140

Cloning a remote tag without using git-fetch-pack directly?

Subject: Cloning a remote tag without using git-fetch-pack directly?

## tl;dr

5 messages between Apr 19, 2011 and Apr 20, 2011.

replies: 4people: 3as markdown or json

Josh Triplett· Apr 19, 2011, 22:20 UTC · lore
Using fetch-pack, I can clone a single tag from a repository:

/tmp/testrepo$ git init Initialized empty Git repository in /tmp/testrepo/.git/ /tmp/testrepo$ git fetch-pack /home/josh/src/linux-2.6/.git refs/tags/v2.6.12 remote: Counting objects: 31617, done. remote: Compressing objects: 100% (21083/21083), done. Receiving objects: 100% (31617/31617), 52.56 MiB | 5.27 MiB/s, done. remote: Total 31617 (delta 12862), reused 19495 (delta 10331) Resolving deltas: 100% (12862/12862), done. keep c80bd1def293bc11591159c96970d8becfe3b2d9 26791a8bcf0e6d33f43aef7682bdb555236d56de refs/tags/v2.6.12 /tmp/testrepo$ git show 26791a8bcf0e6d33f43aef7682bdb555236d56de | head -20 tag v2.6.12

This is the final 2.6.12 release
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBCsykyF3YsRnbiHLsRAvPNAJ482tCZwuxp/bJRz7Q98MHlN83TpACdHr37 o6X/3T+vm8K3bf3driRr34c= =sBHn -----END PGP SIGNATURE-----

commit 9ee1c939d1cb936b1f98e8d81aeffab57bae46ab
Author: Linus Torvalds <torvalds@ppc970.osdl.org>
Date:   Fri Jun 17 12:48:29 2005 -0700
    Linux 2.6.12
diff --git a/Makefile b/Makefile
index 9e005e1..0d1e74d 100644
--- a/Makefile



However, I can't seem to find any way to convince git clone to do the
same thing for me.  git clone will clone a branch, but not a tag.

/tmp/testrepo$ git clone /home/josh/src/linux-2.6/.git -b refs/tags/v2.6.12
Cloning into linux-2.6...
done.
warning: Remote branch refs/tags/v2.6.12 not found in upstream origin, using HEAD instead


On a different note, git fetch-pack seems to silently fail if asked to
fetch a remote tag which points at a tree object rather than a commit
object:

/tmp/testrepo$ git init
Initialized empty Git repository in /tmp/testrepo/.git/
/tmp/testrepo$ git fetch-pack /home/josh/src/linux-2.6/.git refs/tags/v2.6.12-tree
(1) /tmp/testrepo$ echo $?
1


I realize that I want to do something strange here, but it seems like a
kind of strange that git already supports in at least some ways, just
not in others.  Am I missing something?

Thanks,
Josh Triplett
Jeff King· Apr 20, 2011, 06:26 UTC · re: Josh Triplett · lore

Re: Cloning a remote tag without using git-fetch-pack directly?

On Tue, Apr 19, 2011 at 03:20:53PM -0700, Josh Triplett wrote:
Show 5 quoted lines
> Using fetch-pack, I can clone a single tag from a repository:
> 
> /tmp/testrepo$ git init
> Initialized empty Git repository in /tmp/testrepo/.git/
> /tmp/testrepo$ git fetch-pack /home/josh/src/linux-2.6/.git refs/tags/v2.6.12

I don't think there is any reason to use fetch-pack here instead of fetch. The latter will handle other non-git protocols like dumb http and git-over-http.

Show 7 quoted lines
> However, I can't seem to find any way to convince git clone to do the
> same thing for me.  git clone will clone a branch, but not a tag.
> 
> /tmp/testrepo$ git clone /home/josh/src/linux-2.6/.git -b refs/tags/v2.6.12
> Cloning into linux-2.6...
> done.
> warning: Remote branch refs/tags/v2.6.12 not found in upstream origin, using HEAD instead

Right. There is no way to do what you want with clone. The "-b" option is not "just clone this one thing", it is "clone everything, but the branch I am interested in is ...". The fetch refspec remains "+refs/heads/*:refs/remotes/origin/*".

To clone a subset of a repository, you have to do the init+fetch trick, as you did above. If you want the configuration set up by clone, you can do that, too, with "git config". So the equivalent commands to the clone you want are:

  git init linux-2.6
  cd linux-2.6
  git config remote.origin.url /home/josh/src/linux-2.6
  git config remote.origin.fetch refs/tags/v2.6.12
  git fetch origin

We could make clone more flexible with respect to such things, but I don't know how useful that would be. A very small minority of power users want to use weird fetch refspecs, and it is simple enough to configure them manually, as above.

Show 9 quoted lines
> On a different note, git fetch-pack seems to silently fail if asked to
> fetch a remote tag which points at a tree object rather than a commit
> object:
> 
> /tmp/testrepo$ git init
> Initialized empty Git repository in /tmp/testrepo/.git/
> /tmp/testrepo$ git fetch-pack /home/josh/src/linux-2.6/.git refs/tags/v2.6.12-tree
> (1) /tmp/testrepo$ echo $?
> 1

Did you mean v2.6.11-tree? There is no v2.6.12-tree in standard linux-2.6 repositories. So fetch-pack is failing because there are no matching refs to fetch. This works for me:

  $ git init
  $ git fetch-pack /home/peff/compile/linux-2.6 refs/tags/v2.6.11-tree
  remote: Counting objects...
  ...
If you want better error messages, use the fetch porcelain:
  $ git init
  $ git fetch /home/peff/compile/linux-2.6 refs/tags/v2.6.12-tree
  fatal: Couldn't find remote ref refs/tags/v2.6.12-tree
  fatal: The remote end hung up unexpectedly
-Peff
Josh Triplett· Apr 20, 2011, 07:27 UTC · re: Jeff King · lore

Re: Cloning a remote tag without using git-fetch-pack directly?

On Wed, Apr 20, 2011 at 02:26:54AM -0400, Jeff King wrote:
Show 24 quoted lines
> On Tue, Apr 19, 2011 at 03:20:53PM -0700, Josh Triplett wrote:
> 
> > Using fetch-pack, I can clone a single tag from a repository:
> > 
> > /tmp/testrepo$ git init
> > Initialized empty Git repository in /tmp/testrepo/.git/
> > /tmp/testrepo$ git fetch-pack /home/josh/src/linux-2.6/.git refs/tags/v2.6.12
> 
> I don't think there is any reason to use fetch-pack here instead of
> fetch. The latter will handle other non-git protocols like dumb http and
> git-over-http.
> 
> > However, I can't seem to find any way to convince git clone to do the
> > same thing for me.  git clone will clone a branch, but not a tag.
> > 
> > /tmp/testrepo$ git clone /home/josh/src/linux-2.6/.git -b refs/tags/v2.6.12
> > Cloning into linux-2.6...
> > done.
> > warning: Remote branch refs/tags/v2.6.12 not found in upstream origin, using HEAD instead
> 
> Right. There is no way to do what you want with clone. The "-b" option
> is not "just clone this one thing", it is "clone everything, but the
> branch I am interested in is ...". The fetch refspec remains
> "+refs/heads/*:refs/remotes/origin/*".

Oh, good to know; I hadn't yet figured out that -b didn't change the refspec.

Show 15 quoted lines
> To clone a subset of a repository, you have to do the init+fetch trick,
> as you did above.  If you want the configuration set up by clone, you
> can do that, too, with "git config". So the equivalent commands to the
> clone you want are:
> 
>   git init linux-2.6
>   cd linux-2.6
>   git config remote.origin.url /home/josh/src/linux-2.6
>   git config remote.origin.fetch refs/tags/v2.6.12
>   git fetch origin
> 
> We could make clone more flexible with respect to such things, but I
> don't know how useful that would be. A very small minority of power
> users want to use weird fetch refspecs, and it is simple enough to
> configure them manually, as above.

I'd certainly appreciate having a "git clone --refspec=..." or similar, but thanks for suggesting fetch rather than fetch-pack! I actually don't always want the remote set up for this particular purpose, so using fetch works out well.

Show 25 quoted lines
> > On a different note, git fetch-pack seems to silently fail if asked to
> > fetch a remote tag which points at a tree object rather than a commit
> > object:
> > 
> > /tmp/testrepo$ git init
> > Initialized empty Git repository in /tmp/testrepo/.git/
> > /tmp/testrepo$ git fetch-pack /home/josh/src/linux-2.6/.git refs/tags/v2.6.12-tree
> > (1) /tmp/testrepo$ echo $?
> > 1
> 
> Did you mean v2.6.11-tree? There is no v2.6.12-tree in standard
> linux-2.6 repositories. So fetch-pack is failing because there are no
> matching refs to fetch. This works for me:
> 
>   $ git init
>   $ git fetch-pack /home/peff/compile/linux-2.6 refs/tags/v2.6.11-tree
>   remote: Counting objects...
>   ...
> 
> If you want better error messages, use the fetch porcelain:
> 
>   $ git init
>   $ git fetch /home/peff/compile/linux-2.6 refs/tags/v2.6.12-tree
>   fatal: Couldn't find remote ref refs/tags/v2.6.12-tree
>   fatal: The remote end hung up unexpectedly

Well, *that's* embarassing. I thought I'd tested that with a couple of other repositories and tree-tag objects, but apparently not.

Sadly, though, I still can't check out the result:

/tmp/linux-2.6$ git checkout FETCH_HEAD fatal: Cannot switch branch to a non-commit. (128) /tmp/linux-2.6$ git checkout -b master FETCH_HEAD fatal: Cannot switch branch to a non-commit.

I guess I'd hoped for something similar to "detached HEAD" mode.

Based on this, I need still to switch to using faked commits rather than trees.

- Josh Triplett
Jeff King· Apr 20, 2011, 07:38 UTC · re: Josh Triplett · lore

Re: Cloning a remote tag without using git-fetch-pack directly?

On Wed, Apr 20, 2011 at 12:27:20AM -0700, Josh Triplett wrote:
Show 8 quoted lines
> Sadly, though, I still can't check out the result:
> 
> /tmp/linux-2.6$ git checkout FETCH_HEAD
> fatal: Cannot switch branch to a non-commit.
> (128) /tmp/linux-2.6$ git checkout -b master FETCH_HEAD
> fatal: Cannot switch branch to a non-commit.
> 
> I guess I'd hoped for something similar to "detached HEAD" mode.

No, it wouldn't make any sense to have a non-commit in HEAD, detached or otherwise, since you couldn't build commits on top of it (what would the new commit's parent pointer have in it?).

If you just want to check the files out into the working tree, you can do:

  git read-tree --reset -u FETCH_HEAD

But I'm not really clear on what you're trying to accomplish with all of this. If you can describe your ultimate goal, I might be able to help more.

-Peff
Jonathan Nieder· Apr 20, 2011, 07:40 UTC · re: Jeff King · lore

Re: Cloning a remote tag without using git-fetch-pack directly?

Hi,
Some quick additional thoughts.
Jeff King wrote:
Show 10 quoted lines
> To clone a subset of a repository, you have to do the init+fetch trick,
> as you did above.  If you want the configuration set up by clone, you
> can do that, too, with "git config". So the equivalent commands to the
> clone you want are:
>
>   git init linux-2.6
>   cd linux-2.6
>   git config remote.origin.url /home/josh/src/linux-2.6
>   git config remote.origin.fetch refs/tags/v2.6.12
>   git fetch origin

Someone fetching v2.6.12 to build on it today is probably not planning to run "git fetch origin" to fetch the same tag tomorrow. So

	git init linux-2.6
	cd linux-2.6
	git remote add origin ~/src/linux-2.6
	git fetch origin refs/tags/v2.6.12

or even "...; git fetch ~/src/linux-2.6 refs/tags/v2.6.12" could be closer to what is needed.

If for some reason you do want to track how the remote v2.6.12 tag evolves, "git remote" has a funny way to do that:

	git remote add --mirror=fetch -ttags/v2.6.12 origin ~/src/linux.2.6

The documentation calls the argument to -t "<branch>", but in mirror mode it is actually a refspec relative to refs/.

With luck (depending on what you are looking to do), git-new-workdir[1] or "git archive --remote" could also be helpful.

Regards, Jonathan

[1] with the usual avoidable caveats described at https://git.wiki.kernel.org/index.php/SoC2011Ideas#Multiple_work_trees

← back to recent threads