How to Reattach Head in Git
A Git repository can be defined as a group of objects and references. Branches are the objects that we use to present our work. Git also deals with tags that reference commits in the particular branch.
A commit
is likely the state of the source code used at a certain point in some time. Commits
consist of parents and children, as well as whole data about who created the commit and when it was created. Commits
are placed in the repository as objects in the branch.
The HEAD
reference points to the latest last commit in the current branch. The HEAD
pointer is a reference to the currently checked-out branch, and it points to the top of a branch.
However, you can go back in time without checking out a branch. You can use the HEAD
pointer to grab any commit in a branch, and then you can use the index to grab any version of a file easily.
You can use the HEAD
pointer and index pointers to perform the check-out
to a specific commit called a detached HEAD
state in Git. Furthermore, You can check out a specific commit and create a new branch based on a specific commit in a branch.
It won’t become a problem if we only do this once in a blue moon. However, if we repeat it a lot, we will soon start to wonder how to get back to that branch we were working on.
The solution is very simple, as before we check out a branch, we should use the command git checkout master
. This command brings us back to the branch we were working on before the commit was done, but it doesn’t affect the commit we were checking out.
A clean solution would be to set up a new Git repository dedicated to holding the patch series and make it available for others to pull the latest branch at any time.
These situations are rare enough that the usability and performance cost of implementing detached HEAD’s outweigh their benefit, so Git currently lacks this feature. Git can amend commits. However, it is impossible to amend the last commit on a detached HEAD.
Git has a way to permanently delete commits by creating a secret branch, recording the commit data in that branch, and then deleting the commit from HEAD
permanently. However, this feature is only available while a single commit is detached from HEAD
. If a commit has multiple parents, it is impossible to delete it from that branch.
Head Detached in Git
However, when we switch to another branch, the situation is different. If we have our working directory checked out, it is updated to HEAD
, and we can no longer modify the files in it, or we can create some new changes if they don’t conflict with the branch we have switched to.
In this case, HEAD
is detached from the current branch. At the same time, we can use the git checkout
command to change branches without updating our working directory to HEAD
, so HEAD
can be detached at the same time as it’s attached.
It is quite confusing, but it can come in handy if we want to switch between branches without touching the working directory quickly or switch to a different branch and check out a new working directory simultaneously. We can use git checkout -b <newbranchname> <commit>
to do this.
We can perform like this; just check out the branch we desire.
$ git checkout <branch>
For example:
$ git checkout master
If we want to keep the changes we are working on, we should create a new branch or stash our changes in the branch. Any checkout
of a recent commit that cannot be the name of one of our branches will give us a detached HEAD.
When HEAD
is detached, commits look normal, other than no named branch is updated. It is like an unknown branch. For instance, we can say, if we check out a remote branch before tracking it first; eventually, we will end up with a detached HEAD
.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn