detached HEAD is a common situation, sometimes useful, sometimes dangerous. It doesn’t point to any branches, so it will be cleaned by git.
The current commit history is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ git log --oneline --all --graph * 5ea3cec (HEAD -> master) Modify README.md of src * 2b6e826 Same file, same blob * 1ff08f2 Modify README.md. * 8c19a38 Add Copyright notice. * 318c11a Copy css to lib. | * 1134f9e (dev) Make graph more readability | * 71c40d3 Modify README.md in dev branch. |/ * ce4297f Add image. | * 2cb23ac (dev-1.0) README for dev-1.0 branch. |/ * 6fc4b44 (tag: kikoff_tag) Copy doc README.md * 726c6c0 Add source README.md * c8ff9c5 Add README
Create detached HEAD
Create a detached HEAD by git checkout cmd.
1 2 3 4 5 6 7 8 9 10 11 12 13
$ git checkout 8c19a38 Note: checking out '8c19a38'.
You are in'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 8c19a38 Add Copyright notice.
The note of git is important, it provides a lot information.
Check the current HEAD
We can see the current HEAD pointer points to ‘8c19a38’ by the following.