git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* git clone does not checkout active branch
@ 2011-08-03 16:56 Udo
  2011-08-03 20:04 ` Jeff King
  0 siblings, 1 reply; 2+ messages in thread
From: Udo @ 2011-08-03 16:56 UTC (permalink / raw)
  To: git

I have a remote bare repository with two branches 'master' and 'testing', where
HEAD refers to 'testing'. When cloning this repository git checks out 'master',
if 'master' and 'testing' are on the same revision (i.e. HEAD == testing ==
master). Only if 'testing' is one (or more) commit(s) behind or ahead, git clone
checks out the 'testing' branch on the local side. I tried this with git 1.7.5
on Mac OS X (10.6.8).

Addendum: I just tried the same with a non-bare repository:

mkdir A
cd A
git init
touch a
git add a
git commit -m "init repo A with a"
git checkout -b testing

now back in the root dir:

cd ..
git clone A B
cd B
git branch -v -a
* master                 28f599b init A
  remotes/origin/HEAD    -> origin/master
  remotes/origin/master  28f599b init A
  remotes/origin/testing 28f599b init A

it's 'master'! Back to repo A (we are still in branch 'testing'):

cd ../A
touch b
git add b
git commit -m "add b in branch testing"

now 'testing' is one commit ahead 'master'. Now let's clone A again:

cd ..
git clone A C
cd C
git branch -a -v
* testing                23bca39 add b in branch testing
  remotes/origin/HEAD    -> origin/testing
  remotes/origin/master  28f599b init A
  remotes/origin/testing 23bca39 add b in branch testing

You can re-verify this weird behavior by going back to A, checkout 'master' and
merge it with 'testing' (so that all branches have the same head). Now clone A
into D and D will be checked out on master!

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: git clone does not checkout active branch
  2011-08-03 16:56 git clone does not checkout active branch Udo
@ 2011-08-03 20:04 ` Jeff King
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff King @ 2011-08-03 20:04 UTC (permalink / raw)
  To: Udo; +Cc: Junio C Hamano, git

On Wed, Aug 03, 2011 at 04:56:43PM +0000, Udo wrote:

> I have a remote bare repository with two branches 'master' and 'testing', where
> HEAD refers to 'testing'. When cloning this repository git checks out 'master',
> if 'master' and 'testing' are on the same revision (i.e. HEAD == testing ==
> master). Only if 'testing' is one (or more) commit(s) behind or ahead, git clone
> checks out the 'testing' branch on the local side. I tried this with git 1.7.5
> on Mac OS X (10.6.8).

Yes, this is a known issue.

The git protocol just sends the list of refs and the objects they point
to. So the local clone is forced to guess which ref HEAD is pointing to.
E.g., with something like:

  28f599b... HEAD
  1234abc... refs/heads/master
  28f599b... refs/heads/testing

it can see that HEAD is probably "testing". But if it sees:

  28f599b... HEAD
  28f599b... refs/heads/master
  28f599b... refs/heads/testing

then it has to pick one arbitrarily. Our current heuristic is to prefer
"master" over others, and otherwise pick first alphabetically. So it's
at least deterministic, but as you noticed, it's not always right.

The real solution to this would be to extend the git protocol to convey
symbolic ref information (and then wait for both client and server to be
upgraded). Some patches have been floated in the past, but nothing came
of it[1]. Maybe it's time to resurrect them.

As a workaround, you can use "git clone -b testing ..." if you know
ahead of time that "testing" is what you want.

-Peff

[1] See:

    http://thread.gmane.org/gmane.comp.version-control.git/102039

and also:

    http://article.gmane.org/gmane.comp.version-control.git/113567

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-08-03 20:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-03 16:56 git clone does not checkout active branch Udo
2011-08-03 20:04 ` Jeff King

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).