Detached HEAD State
What is HEAD?
A HEAD is known as a so-called “pointer”. By default, it refers to the latest commit found within a branch. This does not necessarily mean that a HEAD can be found within the master branch. It can also be found within a feature branch as well.
Usually you’ll be able to see this via the
git log command.
Found on the side of one of your commit’s SHA-1, you are able to see where your current HEAD is pointed at. Currently my HEAD is pointed at my local repository’s add-form branch. This signifies that the latest commit within my local repository is found within my add-form branch.
In the case of your HEAD found within a feature branch instead of your master branch, it signifies that you have not merged your branches yet in both remote and local repositories. Your HEAD will point to your master branch after it has been merged in both remote and local repositories.
Both remote and local repositories may differ in HEADs. Meaning, a HEAD in a local repository may be ahead of your remote one. This happens because you haven’t pushed a commit to your remote repository.
You are able to locate the HEAD of your remote repository by finding the origin/HEAD tag found at the side of your commit’s SHA-1.
What is a detached HEAD state?
A detached HEAD state happens only when you
git checkout an old version of your project. You can do this by doing:
git checkout old_SHA1
What happens in a detached HEAD state?
In a detached HEAD state you are able to rewrite commits and change them in any manner you’d like. However, if you make changes to your old commits and then commit, these changes are not stored anywhere. Meaning, they don’t go to any of your branches.
Revisting past commits
In the case you do want to revisit a past commit, you’d have to create a new branch with your past commit’s SHA-1.
git branch feature_branch old_SHA1
The code above would look like this:
git branch viewing_branch 57f347409f5b1997de759d5862448e269bd21c1c