Signup/Sign In

How to Reset to Head in Git

A lot of times, we will end up making changes in our repository that we don't need. This could be making incorrect commits, or making commits on the wrong branches, or accidentally staging files that we don't need in the next commit. Git provides us with a very powerful command called Git Reset that can help us undo changes in our repository. Let's learn how to use this command to reset our repository.

Git Reset

Git Reset

The Git Reset command can be used to undo changes. These changes could be present in the repository, the staging area, or the working directory. Git Reset has three modes - soft, hard and mixed. Git Reset essentially rewrites the history of our project.

Git Reset --Soft

The --soft mode is used to reset changes made to the repository. It works by changing the position of our HEAD to point to the previous commits. But it will keep the staging area and the working directory as it is.

Git Reset --Mixed

The --mixed mode will undo changes made to the repository and the staging area. This is also the default mode used by Git Reset.

Git Reset --Hard

The --hard option will undo changes from the repository, the staging area, and the working directory. This option should be used carefully, as we can lose unsaved changes.

Git Reset command and the three modes in which it works

The general syntax of using this command is shown below. We can use the hash of the commit point to which we want to roll back.

$ git reset <option> <commit-hash>

We can also specify the commit relative to our current HEAD by using the HEAD~n notation. For example, HEAD~2 will reset our repository to the second commit before our HEAD.

$ git reset <option> HEAD~n

Git Reset to HEAD

The most recent commit on our currently checked-out branch is called the HEAD. We might have made changes to our files after the last commit but we now want to discard these changes. We may want to reset our staging area or the working directory back to our HEAD.

To reset our staging area back to the HEAD, use the --mixed option. This is also the default option so we can simply enter Git Reset.

$ git reset --mixed HEAD

To reset both the staging area and the working directory to the HEAD, use the --hard option.

$ git reset --hard HEAD

Consider the following example to better understand how to reset to HEAD.

Suppose, we have three files in our repository and the changes in two of them were committed and the third one is still untracked. The status of our repository after this commit is shown below. Our HEAD also points to this commit. The content of the files is also shown below.

Status of the repository after the commit

Content of the files after the commit

After the last commit(HEAD), we stage the file f1.txt and also modify f2.txt. File f3.txt is left untouched.

Modifying files and adding some of them to the staging area.

Content of the files after the commit

Now, if we need to reset just the staging area, then we can simply use the Git Reset command. The staged file(f1.txt) will be removed from the staging area, but the changes made to it(working directory) are still present.

Using Git Reset to unstage the changes made to the files.

The staging area was restored after running the Git Reset command.

Viewing the content of the file after resetting just the staging area.

If we wish to reset the working directory as well, then use the --hard option with it. We can see that all the new changes that were made to the files are also lost and our repository looks the same as it was just after the commit.

Git Reset --hard to the HEAD

The status of the repository becomes the same as it was just after the commit.

Unsaved changes of the files were removed.


We will often end up making changes in our repository that we may not need. The Git Reset command is used to reset the repository to a previous commit. Git Reset has three options - soft, mixed, and hard. The --hard option must be used with caution as it will restore the changes of the working directory and we may lose unsaved changes.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight