Make Selenium Easy

Git Tutorial 30 – Understand Soft, Mixed And Hard Options Of Git Reset

Introduction

As a part of GIT Tutorials End To End, we will learn about –soft, –mixed and –hard options of the git reset command.

Prerequisite posts

I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.

I have already covered the basic usage of the git reset command in this post. In this post, we will see a detailed explanation of the –soft, –mixed and –hard options of the git reset command.

State Management Systems In Git

We have already learned about Working Directory, Staging or index area and Remote or HEAD. I will give a quick recap of these terms here. I would like to give a real-time example here to make you understand better.

You work as a Fruit vendor and you are famous for selling good quality fruit in the market. You produce fruit on your farm (Working Directory). Since you sell only good quality fruits, you choose the best from the farm and keep them aside (Staging Area). You pack all good fruits in one or more boxes (Commits) and load them in a truck (Local Repository).

Working Directory

We create a new git repository or clone an existing repository in local. We can add/delete/modify files in these repositories. Initially, all these changes are in the local working directory. For example, If we add a file to this git repository then the git status command will immediately reflect changes. So any change in the repository will be in the working directory first.

Staging or Index Area

When we make any changes in the git repository that will be in the working directory first. If we think that this change should go as a commit to the repository then we need to add this change to another intermediate area called staging or index area. We use the git add command to add changes from the working directory to the staging area. We can always remove changes from the staging area as well. Remember that adding changes to the staging area does not mean that it will be removed from the working directory.

Remote Area or HEAD

Once we have added changes to the staging area then these changes are ready to be part of the next commit and can be pushed to the remote repository (GitHub/BitBucket etc). Once we do commit then the HEAD of the local branch will be updated. After pushing the commit, the HEAD of the remote branch will also be updated.

Git reset command

Git reset command is used to reset HEAD or undo a commit. We have three options to use with the git reset command – soft, mixed and hard in which mixed is the default option. Before we go into detail let’s create a git repo with some commits. I will not push these to the remote repo as of now.

The status of the above git repository can be shown in the image below –

So above we have two commits and want to reset/undo the last commit. How we want to undo that depends on the option we choose.

Git reset –soft

So the process is to add the change to the staging directory from the working directory and commit it before pushing it to the remote repository. If you would like to just undo the commit command and do not want any change in the working directory and staging area then use the –soft option. In the last commit, I added the second line in fileA.txt and then used the git add command to add the change in staging and then used the git commit command to commit. The git reset command with the –soft option will just undo the last command i.e. git commit. You will be in the same position after you used the git add command or before the git commit command. The changes done as part of the second commit will be present in the working directory and staging area.

Let’s do this in action –

You can see that after running the git reset command with the –soft option, the last commit disappeared/was undone but changes are still in the staging area and working directory. HEAD is set to the previous commit i.e. first commit in my case above.

Git reset –mixed

If we want to go one step back further from what we have done in the git reset –soft above then we should use the –mixed command. This option will undo the git commit and git add command. In short, the state of the staging area will be the same as the local repository. The changes done as part of the second commit will be present only in the working directory.

Let’s do this in action. Please note I have restored the repository status as in beginning.

You can see the last commit and git add commands were undone and changes are still in the working directory but not in the staging area.

Git reset –hard

If we want to go one step back further from what we have done in the git reset –mixed above then we should use the –hard command. The –hard option will remove all changes done as part of the removed commit. The contents of the local repository, staging area and working directory will be the same.

Let’s see this in action –

You can see above that all changes done as part of the second commit were removed. HEAD is set to the previous commit i.e. first commit in our case.

The git reset command with any option will reset HEAD. So –

  1. If you just want to reset HEAD and keep staging and working directory as it is then use the –soft option.
  2. If you want to reset HEAD and staging area but the working directory as it is then use the –mixed option.
  3. If you want to reset HEAD, staging and working directory then use the –hard option.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Git Tutorial 30 – Understand Soft, Mixed And Hard Options Of Git Reset

by Amod Mahajan time to read: 4 min
0
%d bloggers like this: