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.
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.
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.
After the last commit(HEAD), we stage the file f1.txt and also modify f2.txt. File f3.txt is left untouched.
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.
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.
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.