Advertisement

Responsive Advertisement

Detached Head in Git

 Detached head in Git:

Before knowing about detached head,you need to know about git objects and refs.

A Git repository is a collection of objects and references. Objects have relationships with each other, and references point to objects and to other references. The main objects in a Git repository are commits, but other objects include blobs and trees. The most important references in Git are branches, which you can think of as labels you put on a commit.

HEAD is another important type of reference. The purpose of HEAD is to keep track of the current point in a Git repo. In other words, HEAD answers the question, “Where am I right now?”

For instance, when you use the log command, how does Git know which commit it should start displaying results from? HEAD provides the answer. When you create a new commit, its parent is indicated by where HEAD currently points to.

You’ve just seen that HEAD in Git is only the name of a reference that indicates the current point in a repository. So, what does it mean for it to be attached or detached?

Most of the time, HEAD points to a branch name. When you add a new commit, your branch reference is updated to point to it, but HEAD remains the same. When you change branches, HEAD is updated to point to the branch you’ve switched to. All of that means that, in these scenarios, HEAD is synonymous with “the last commit in the current branch.” This is the normal state, in which HEAD is attached to a branch.

A visual representation of our demo repository would look like this:

After detached head


After reading below reference link.If you have a doubt of how to get back changes to master instead of keeping them in branch.Perform below steps:

1.checkout to master and do hard reset to move head 1 step back ward.

2.Merge branch so created to master.

References:
https://www.cloudbees.com/blog/git-detached-head

Post a Comment

0 Comments