Git Reset Head

In Git, the term “HEAD” refers to the current branch’s latest commit or you can say that to the tip of the current branch. To undo changes or revert to a previous state for a particular branch, resetting to HEAD is a common method. Resetting to HEAD can help you discard changes in your working directory, unstaged changes, or even move the branch pointer to a different commit.

In this article, we will cover various approaches to reset to HEAD in Git.

QUICK NOTE ON HEAD

  • HEAD can also be used to refer to the current working directory state.
  • If there are some changes in your project which are not committed. You can do git diff HEAD, and it will show the difference between the last commit and the working directory.
  • Resetting the head means resetting to the latest commit.
  • You can use a number with a head to go to a particular commit. For example, HEAD means latest commit, HEAD{1} means one commit before head, and similarly HEAD{n} means n commit before head.

How to Reset to HEAD

There are primarily three modes to reset to head with different purposes.

Mode 1: Hard Reset

git reset --hard HEAD

This mode is preferred when you want to undo the commit and remove the changes from staging and working directory. Use this command with caution because the changes gone cannot be reverted back. It updates both the index and the working directory.

Mode 2: Soft Reset

git reset --soft HEAD

If you want to undo your commit but leave the changes as staged than this soft mode is preferred. It leaves the working directory and index untouched and move to the head.

Mode 3: Mixed Reset

git reset --mixed HEAD

This is the default mode of git reset command. If you want to undo the commit and remove your changes from the staging but do not remove them from working directory this mode is preferred. It leaves the working directory untouched but changes the index.

BONUS POINT: Using this reset command, you can switch to any other commit instead of head.

git reset commitId

Contact Us