Must HEAD point to a branch, or can it point directly to a commit node? Is it possible to move HEAD to point to an arbitrary commit node even if no branch is referencing this node?
Asked
Active
Viewed 456 times
2 Answers
3
Must HEAD point to a branch, or can it point directly to a commit?
HEAD is just a pointer to a commit. It is possible that no branch points to that commit.
Is it possible to move HEAD to point to an arbitrary commit even if no branch is referencing this commit?
Yes, it is. Just do:
git checkout <commit-sha>
and you will check out that commit in a detached head state. That basically means that you are currently not on a branch.

Cristian Lupascu
- 39,078
- 16
- 100
- 137
-
1Thanks. Can I substitute a SHA1 of a commit for `
`? – Evan Aad Apr 06 '17 at 13:29 -
Is it possible, then, to delete all branches from a repository, including `master`, and work solely with `HEAD`? – Evan Aad Apr 06 '17 at 13:31
-
1I just tried it now and yes, it is possible. Now when I do `git branch -a` I only get `* (HEAD detached from ebe50df)` listed as a branch. – Cristian Lupascu Apr 06 '17 at 13:33
-
But HEAD cannot be deleted, right? Even if there is one or more other branches in the repository. – Evan Aad Apr 06 '17 at 13:35
-
1I don't see how that would be possible. – Cristian Lupascu Apr 06 '17 at 13:39
1
Read this full answer:
How to move HEAD back to a previous location? (Detached head)
HEAD can be point to any commit, If HEAD is not pointing to a the latest commit in the branch you are in a detached HEAD.
Read the above answer to get the full detailed what is it and how to "fix" it.

Community
- 1
- 1

CodeWizard
- 128,036
- 21
- 144
- 167
-
Thanks. When you clone or initialize a repository, is it possible to specify that it be created without any branch references, not even `master`, except for HEAD? – Evan Aad Apr 06 '17 at 13:43
-
1nope but you can thsn detach any branch and delete all your history – CodeWizard Apr 06 '17 at 13:56
-
-
1HEAD is simply a pointer to commit you cant rename since its not a real branch – CodeWizard Apr 06 '17 at 14:22