DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

Fix a Detached HEAD - Git

Solution

Just checkout to the branch you want:

$ git checkout <branch>
Enter fullscreen mode Exit fullscreen mode

For instance:

$ git checkout master
Enter fullscreen mode Exit fullscreen mode

Note:

If you want to keep the chances you are working you should create new branch or stash your changes.

Reason

Any checkout of a commit that is not the name of one of your branches will get you a detached HEAD. A SHA1 which represents the tip of a branch still gives a detached HEAD. Only a checkout of a local branch name avoids
that mode.

When HEAD is detached, commits work like normal, except no named branch gets updated. It is like an anonymous branch.

Detached HEAD

For example, if you checkout a remote branch without tracking it first, you can end up with a detached HEAD.

For more info: Visual Git Reference

All done!

Top comments (0)