How to Reset HEAD in Git

Abdul Jabbar Feb 02, 2024
  1. Git Head Reset
  2. Git Head Reset --soft
  3. Git Head Reset --mixed
  4. Git Head Reset --hard
How to Reset HEAD in Git

Git helps us in many aspects while working on shared repositories.

We can perform many functions using Git, as we can create a new branch, merge a branch, remove a branch, as per our requirements. These actions can be accomplished through multiple Git commands.

The command git reset is a compound and multisource feature for undoing changes. This command can be used with three primary options: --soft, --mixed, --hard.

These three arguments correspond to Git’s management mechanism’s called The Commit Tree (HEAD).

The HEAD points out the last commit when the Git checkout branch is applied. Furthermore, we can also say that Git HEAD points to the current branch’s last commit.

When we check out a branch or make a new branch, Git HEAD transfers the latest commit to our local branch. More precisely, HEAD is a pointer that always points for the current commit, and it may or sometimes may not belong to the current branch.

Git Head Reset

When we have saved our changes to the desired repository, we should be relaxed that we have used the git reset command to return to those changes that are done, it will make our current branch back to its original place where it was before the command ran.

The option HEAD releases the new branch. We can say that what git reset-hard HEAD can do is trash all the changes we have done that aren’t committed.

The git reset command can be used in combination with other commands:

  • Transfers what the HEAD branch points toward.
  • To make it look like a tree object.
  • To look like the current working folder.

And, there are two different categories of git reset.

Git Head Reset --soft

This Git command will reset the HEAD. But our index and working directory will not be affected in any way.

We can use the Git reset command option to reset the head of a local branch:

git reset --soft

Git Head Reset --mixed

The git reset --mixed command will change the head location to the specified commit, and further, it will delete the changes from the staging area. This is an example of undoing the changes.

So if we run the Git reset HEAD command, it will move the HEAD back to the first parent commit again. The syntax for the above-discussed situation is as follows:

git reset --mixed or $ git reset

Git Head Reset --hard

This command can be a hurdle for us. It can only be used if we exactly know how to use it.

When we use git reset-hard HEAD to restore to the last commit becomes an issue for us. Fortunately, we developers have a better solution for its correction.

Note that git reset --hard is a threatening command. It can smash all our non-committed modifications. We should check it first and make sure that the Git status output is clean before dealing with it.

This command will remove the commits stored in the index if we want to remove all commits in the index and the staging area (we need to undo our last commit and the last commit before that). We can use the Git reset command with the --hard option:

git reset --hard HEAD~2

This command will remove any commit from the index and the staging area. It will also remove the commit from the history.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

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

Related Article - Git Reset