Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
What is Head in Git?
Sometimes, you see the Git documentation referring to something called HEAD. For example, The branch should be completely integrated in HEAD. But what exactly is Git HEAD? In this article, we’ll get to know more about Git HEAD but before that let’s just summarize what Git is and what its used for. Git is a tool for distributed control not only used by product managers and developers but also data scientists to manage the source code development of the program and its history.
HEAD Pointer in Git
Git maintains a variable for referencing, called HEAD to the latest commit in the recent checkout branch. You can imagine HEAD as the “current committed branch”. And we can think of as a pointer, as the purpose of this variable is to point to or act as a reference to a particular commit in the repository. Say, we make a new commit in the repo then the pointer or HEAD is going to move or change its position to point to a new commit.
HEAD points to the starting point of the present branch in the repository at all times. It can be thought of as the last state or the last checked out point in a repository. In other words, the HEAD is a pointer to the next commits’ parent or where the next commit is going to happen as that’s where the repo left off.
A good analogy would be a record player and the playback and record keys on it as the HEAD. As the audio starts recording, the tape moves ahead moving past the head by recording onto it. Stop button stops the recording while still pointing to the point it last recorded and the point that record head stopped is where it will continue to record again when Record is pressed again. If we move around, the head pointer moves to different places, however, when Record is pressed again starts recording from the point the head was pointing to when Record was pressed.
In Git, you can use the command below to see what the HEAD pointer points.
cat .git/HEAD
Whenever we make a new commit like shown below, it gets added before the current HEAD which makes Git automatically points the HEAD to the new commit.
git diff HEAD..HEAD~3
git log @
1. Detached HEAD
It is plausible for HEAD to point to a specific change that has not been linked to a branch name yet. This is the situation which is called a detached HEAD and it happens when someone checks out something other than a (local) branch, say a specific commit, a remote branch, or a tag. Detached HEAD, therefore, can be used to checkout a commit that isn’t pointing to the starting point of any existing branch, or to create a brand new commit which isn’t necessarily referenced by a known branch.
Let’s take an example where we checkout commit b in one or the other way
git checkout master^^
OR
git checkout v3.1
Note: that no matter whichever checkout command is used, HEAD will now refer to commit b. This status of b is called as detached HEAD state.
Let’s look at the examples below to see what happens when a commit is created:
git checkout -b foo
First a new branch named foo is created, which is referred to commit f which in turn updates the HEAD to point to branch foo. This means that it will not be in a detached HEAD state any longer.
git branch foo
This creates a new branch named foo, that is referred to commit f, but the HEAD is left detached.
git tag foo
This too creates a new tag named foo, which is referred to commit f, but the HEAD is left detached.
Suppose, you changed to a position other than commit f, then the object name must be recovered first (typically done by using the git reflog command), and after that, a reference is created to it.
To find out the last two commits HEAD referred to, use either of the below commands:
git log -g -2 HEAD
OR
git reflog -2 HEAD
2. ORIG_HEAD
There is one more kind of HEAD that you need to know about. The commands “merge” or “pull” always left the original tip of the current branch in something called ORIG_HEAD. It can be used using the following commands:
git reset –hard ORIG_HEAD
Using this, reset hard brings the index file along with the working tree back to its original state, while resetting the tip of the branch to that commit, but it discards the local changes.
git reset –merge ORIG_HEAD
But what if you want to keep the local changes, in that you can use the above command to keep the local changes.n addition, merge always sets ‘.git/ORIG_HEAD’ to the original state of HEAD so a problematic merge can be removed by using ‘git reset ORIG_HEAD’. In addition to this, merge sets ‘.git/ORIG_HEAD’ to the original state of HEAD at all times so as to remove a problematic merge by using ‘git reset ORIG_HEAD’.
If you face any problem with multiple commits, ORIG_HEAD is set to the starting point of the current branch before applying any patches as an error in the commits can be more easily fixed this way.
Advantages of Git HEAD
Conclusion
Git has many uses and is extensively used by developers, product managers, and data scientists. Its commands are very effective and can be very useful. HEAD is a reference to the last commit in the currently checked-out branch.
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.