What is Git Reset Command?
Git Reset Command
A lot of times, we will make incorrect commits or make commits to some other branches or we might need to delete a commit or any change that we had made previously. The Git Reset command is used to reset or undo changes in Git. These changes can be made to the HEAD, the working directory, or the staging area. Let's try and understand how Git Reset works.
Reset Changes
- Often times we will find ourselves in situations where we have to undo certain changes. Git Reset command helps us in doing that.
- As we know, changes can be made to the Working Directory, the Staging Area, or to the Repository. Git Reset has three options for reverting changes in each of these stages.
- We can think of Reset as the reverse process of normal Git Workflow in which we move from the Working Directory to the Staging Area and finally commit changes to the repository.
- A thing to note here is that we may lose reference to some commit points when resetting as the branch reference pointer is also changed along with the head. Consider the following example in which after a reset is performed to revert back to commit B, the commit point C is no longer referenced by any pointer.

Git Reset Command
To reset to a previous commit we can use the commit hash of that commit.
$ git reset <option> <commit-hash>We can also enter the position of the commit relative to our current HEAD. In the following command, Git will reset to the n-th commit before head.
$ git reset <option> HEAD~nWorking with Git Reset
Let's try to understand the three modes of Git Reset with the help of an example.
Consider we have a repository with a file called f1.txt which has the text "First Line" and these changes are committed.

Next, we add a line to this file and stage and commit this change.


Now, let's add another line to the file and only stage that change. Add one more line and but don't stage that change.


Time to compare the three Git Reset options. First, let's look at the state of our repository.

When we run git reset --soft HEAD~1, it will make the HEAD point to the commit just before our HEAD but will keep the staging area and working directory intact.


Now if we run git reset --mixed, the staged changes to the file will be lost but the working directory still has the changes. This is evident from as we can still see the unstaged file when we run Git Status.

Lastly, if we run git reset --hard, even the working directory is restored to the initial version corresponding to the initial commit. We can that the contents of the file are the same as they were after the initial commit.

Summary
We would often find ourselves in a situation where we need to go back and undo our changes. The benefit of version control systems is that we can do this if we have committed those changes previously. Git Reset is the command that helps us reset the state of the repository. This command has three options - soft, mixed and hard and they can be used according to the needs of the developer. We must be careful while using the --hard option as all unsaved changes(working directory) are also reverted to match the previous commits. Another thing to be aware of is that we might lose reference to certain commit points while resetting. Overall it is a very valuable command but should be used thoughtfully.










