1

I just cloned a repository containing a set of different branches. When I type git branch it gives:

$ git branch

* rel/next/master

The content of .git/HEAD in the local cloned repository is:

* rel/next/master

Normally when I clone a repository its always on refs/heads/master as the default branch. Does the above mean that someone manually at some point changed the value of the remote HEAD? Or does git compute the default/remote HEAD automatically based on the names of the branches somehow?

I have read through this:

How does origin/HEAD get set?

And have tried to see if it was necessary to "prune" ambiguous heads, but that is not really the case here.

I guess I could just change the content of HEAD on the remote git repo. But I would like to know how its possible that it has this value first

Community
  • 1
  • 1
u123
  • 15,603
  • 58
  • 186
  • 303

2 Answers2

3

Edit: I'm not sure I answered the intended question (having followed the link to the other question since then). If you're wondering: how did the remote repo's HEAD get changed to identify something unusual (if indeed it did), the answer depends on who uses the remote repo, and how. If the remote repository is used for git checkout operations (e.g., to deploy stuff even from a bare repository), the HEAD entry can be updated by a checkout.


When you do an initial git clone, you specify a branch with -b, and git checks that out.

If you don't specify a branch with -b (most people don't), git still picks something to check out. What it picks to check out, is a bit tricky.

After you've cloned, you can run git ls-remote origin. You will see something like this:

6c4ab27f2378ce67940b4496365043119d7ffff2        HEAD
32f56600bb6ac6fc57183e79d2c1515dfa56672f        refs/heads/maint
6c4ab27f2378ce67940b4496365043119d7ffff2        refs/heads/master
9eef2c89753895da807c936ff1ba3a255c8370c9        refs/heads/next
0c53c9308fc7349ea93fd96ce7bbff7a8d0dfa41        refs/heads/pu
3d84dcf9136e737c8a8d10b7954928df732dadca        refs/heads/todo

(this is from running git ls-remote on the git repository itself just now). Note that there is a raw SHA-1 listed for HEAD, not the symbolic reference, even though HEAD on the remote is almost certainly a symbolic reference (specifically to refs/heads/master).

Your local git compares the SHA-1 here to the SHA-1s for all the refs/heads/ (branches). If exactly one matches, as is the case here, it assumes you wish to check out a local branch that tracks the corresponding remote branch.

If more than one SHA-1 matches, git just picks one of them.

If none match, I'm not actually sure what git does.

torek
  • 448,244
  • 59
  • 642
  • 775
  • For information, [this answer](http://stackoverflow.com/a/8841024/2541573) explains what happens when origin/HEAD becomes dangling or points to a commit that multiple branches also point to. – jub0bs Aug 21 '14 at 07:46
  • @Jubobs: Jefromi suggests what he thinks it does, which may be correct, but to find out for sure, we would have to try it, or read the git source (and both methods only show what a particular version of git does: it may be version-dependent). – torek Aug 21 '14 at 07:50
0

If it's on github, there's a field in the "settings" page of a repository to change the default branch to whatever you want.

If it's on a regular bare repo that people usually clone from, you can use git symbolic-ref to update HEAD to point to any reference that you want and that will be the default branch that people get when they clone the repository.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169