Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git clone does not checkout active branch

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!

like image 622
Udo Avatar asked Apr 16 '26 09:04

Udo


1 Answers

git clone copies all branches and their history, and the SHA1 and not refs of their heads. AFAIK refspecs are not transferred because they are newer to git than cloning (obviously), and the clone protocol does not preserve them... this means that it guesses (edit: this has been confirmed by the mailing list).

The ref for head is guessed during clone by matching its SHA1. In your case, the SHA1 is ambiguous because master and testing both match, so master is chosen by convention. This is not documented anywhere to my knowledge, but should be.

To see that this is so, go to your B example repo and run git ls-remote. The output will give you SHA1s, and not refs:

1c3eebcd1bd3659a40f02880918d5fbd5614b51a    HEAD
1c3eebcd1bd3659a40f02880918d5fbd5614b51a    refs/heads/master
1c3eebcd1bd3659a40f02880918d5fbd5614b51a    refs/heads/testing

As a workaround,

git clone -b <branch_name>

will clone and checkout your desired branch branch_name, such as master or test.

I think at this time you are forced to stick to the convention that HEAD is master and master is the default branch for checkout, and if you are set on having a different head for devs to clone use the -b switch.

like image 156
shelhamer Avatar answered Apr 18 '26 01:04

shelhamer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!