4

How can I detect if the current HEAD is an unborn branch?

"git rev-parse HEAD --" outputs

fatal: bad revision 'HEAD'

But git also outputs this for all other inputs like "git rev-parse noHEAD --".

"git branch" returns an empty output.

Do I have to read .git/HEAD and check if the contents start with "ref: " and the reference does not exist?

MrTux
  • 32,350
  • 30
  • 109
  • 146

1 Answers1

3

But git also outputs this for all other inputs like "git rev-parse noHEAD --".

The difference is that HEAD must exist, by definition, in a repository.

If you have a HEAD that is not a valid revision, then it must be pointing to a branch that is yet to be born.

For example:

> git checkout --orphan newbranch
> git rev-parse HEAD --
fatal: bad revision 'HEAD'

Versus if you have a branch checked out:

> git checkout master
> git rev-parse HEAD --

Versus if HEAD is nonexistent or damaged:

> git rev-parse HEAD --
fatal: Not a git repository (or any of the parent directories): .git
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187