higgs241
higgs241

Reputation: 629

How to tell if your head is detached in git?

How do you tell if your head has been detached in git? I have a feeling that might be the root of my problem.

Upvotes: 15

Views: 7219

Answers (4)

banna
banna

Reputation: 249

For git version 2.22 and above, the command git branch --show-current can be used. In case of detached head state, the output will be nothing.

If you want to use it in a script, we can incorporate it with wc as follows:

git branch --show-current | wc -l

This command will return 0 in case of detached head, and 1 in case of a valid branch.

Upvotes: 6

memoselyk
memoselyk

Reputation: 4118

An alternative useful for scripts, without having to deal with $GITDIR is

git rev-parse --abbrev-ref --symbolic-full-name HEAD

This gives me HEAD for a detached head, or the branch name on the other case.

Upvotes: 28

Balog Pal
Balog Pal

Reputation: 17163

git status is supposed to tell you everything relevant.

manually you can look content of .git/HEAD file. If it has a hash, you're detached. if there is a ref like ref: refs/heads/master you're not.

Upvotes: 4

Ju Liu
Ju Liu

Reputation: 3999

git branch

would show

* (no branch)
  master
  develop

If you are in detached state.

Upvotes: 6

Related Questions