21

I'm wondering what's the purpose of HEAD in bare repository? Is it just used when repository is cloned to know which branch to checkout in cloned repository?

Please note, this question is not about what is HEAD, but given its functions I'm wondering why it's needed inside bare repository.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • possible duplicate of [What is HEAD in Git?](http://stackoverflow.com/questions/2304087/what-is-head-in-git) – Abimaran Kugathasan Mar 27 '15 at 08:29
  • 1
    I guess that this is becuase, you can push commit (not branch) to remote, and then you need some starting point of DAG – Luka Rahne Mar 27 '15 at 08:32
  • @LukaRahne, yeah, but `HEAD` doesn't point to commits, it points to branch refs, so even if I push commits the starting point can be found using branch refs. – Max Koretskyi Mar 27 '15 at 09:04
  • I am not familiar with this, but I guess that if i checkout commit (localy) head will point to commit, not branch. – Luka Rahne Mar 27 '15 at 09:23

1 Answers1

12

Can you please name a case where HEAD is used in a bare repository?

See "t/t1430-bad-ref-name.sh"

You can do a soft reset in a bare repo, which means you can move HEAD (current commit) around, which is one way to change the default branch.

That means that when you clone a bare repo, the default branch checked out in the non-bare clone will be the one referenced by the bare repo HEAD.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    thanks, so basically my this assumption is correct _Is it just used when repository is cloned to know which branch to checkout in cloned repository?_ ? I'm also wondering why you specified _HEAD (current commit)_, while HEAD usually points to a branch ref even in a bare repository. And what does soft-resetting of `HEAD` do? Move the branch ref that `HEAD` points to? – Max Koretskyi Mar 27 '15 at 09:25
  • 1
    @Maximus because HEAD doesn't always reference a branch. It can also just reference a commit, in which case this is a "detached HEAD": http://stackoverflow.com/a/3965714/6309 – VonC Mar 28 '15 at 11:06
  • Note to self: this is my 1000th Enlightened badge (https://stackoverflow.com/help/badges/19/enlightened?userid=6309) – VonC Mar 09 '20 at 21:14
  • When we clone from the bare repo, we see an `origin/HEAD`, but why isn't it seen when we clone from a repo hosted on Github? – huggie Oct 24 '22 at 05:28
  • 1
    @huggie Because [GitHub defines a default branch](https://stackoverflow.com/a/6838756/6309) (which a bare repo does not have by default, as [mentioned here](https://superuser.com/a/1192881/141)) – VonC Oct 24 '22 at 06:01