Git Reset Command – Soft, Mixed, Hard

Git Reset Command - Soft, Mixed, Hard

In this tutorial, we will talk about the git reset command and its flag like soft, mixed, and hard and how you can use it with command. We will cover all in detail with the example command. So let’s go ahead.

The git reset command to use when we want to get back to the previous commit of the repository, and not including any changes made after that commit. For the previous commit, we have a git commit history and we can get back.

It is common when you are working in a team on the same project and creates multiple branches, add files and then commit when you are done with the task.

So in some cases, you have committed the wrong file, perhaps you committed the wrong branch, or could be anything wrong.

Maybe you realize that your changes are not correct and you have other modified files that have a new couple of lines of code. Or you can say you lost some lines of code in files then you want to get back to the previous state.

So we will use the git reset command to revert HEAD. You can also say reset to HEAD on the initial state or at a specific commit state.

So let’s get started and learn about the git reset command using the flag soft, mixed, and hard.

Git Reset

As I said above, the git reset command is used to get back to the previous commit of the branch/repository. You just have the commit hash number. The commit hash number is a unique number generate while committing the files.

If you don’t know the commit hash number then you can find it by following the steps.

  1. Find the previous commit
  2. Move the repository back to that step

Git Reset Command

In the first step, we will find the previous commit in the git commit history where we want to move back. So for this, we will use the git log command to get the full details of all commits. See the following command.

This command will give a large number of details of commits and here you can get the commit hash number of a specific commit.

But you need only that commit hash number and don’t need any long details of commit. So, to make a short log list, just use the --oneline flag with the git log command. See the following example.

Output:
45g895r (HEAD -> master) files upload to github
34896g4 updated the login file...
983745f user verification on signup page...
35tu578 signup file updated.

As you see the output of the above command is a single line of the short messages. So the first seven characters of our commit hash number that we exactly need to revert back on that step. The rest part is the commit messages.

Suppose we want to return back to the 35tu578 commit step. So we will run the git reset command like the following.

So this above command will take you to that state of commit hash (when you have the signup file updated.)

Git Soft Reset

git reset --soft command we used when we want to get back the last git commit. The –soft will preserve the changes done to your files. It will not modify the working directory or index file.

So, you just need to specify the commit with the command to move back to HEAD then it will remove the last git commit from your git history.

OK.

So, use the git reset command to move on to specify HEAD, and don’t forget to add --soft flag option and HEAD specify where you want to return. See the following example commands.

To move back to HEAD

Both commands are the same work to move back to the commit just before the HEAD

OR

To move back two commits before the HEAD

The above commands will not make any changes in your working tree and index of the file. So the modifications made between the original and current HEADs will be staged.

Let’s see with an example.

Check the git log to see the history and then execute the git reset command with --soft flag option to a specific HEAD.

Output:
45g895r (HEAD -> master) files upload to github
34896g4 updated the login file...
983745f user verification on signup page...
35tu578 signup file updated.

To move to the before one commit the HEAD, run the following command.

OR

Git Mixed Reset

git reset --mixed command we used to update the index to HEAD. If we use git reset, git will not stop after updating whatever HEAD is pointing to. It will also update the index to HEAD.

So when we use the --mixed flag with the git reset command, it will update the index (staging area) to HEAD. It will not update the working directory. This is the default action. See the following example commands.

To move back to HEAD

Both commands are the same work to move back to the commit just before the HEAD

OR

To move back two commits before the HEAD

So use these commands to make action for not updating the working directory but will reset the index staging area to HEAD.

Git Hard Reset

git reset --hard command used to reset the index file and the working tree. Normally we use the git reset command to move from the current HEAD to the specific HEAD.

So when we use the --hard flag option with the command then it means, it will update the working directory to the index file. And you will be left with your working directory’s untracked files. See the following example commands.

To move back to HEAD

Both commands are the same work to move back to the commit just before the HEAD

OR

To move back two commits before the HEAD

The above commands will reset the working tree and index of the file.

Let’s have an example.

You may have a global overview of your current Git branch and its commits by using the git log command. So let’s run the git log command to check the commit history.

Output:
45378g7 (HEAD -> dev, origin/dev) dev branch commit
45g895r (origin/master, master) files added to github
34896g4 updated the login file...
983745f user verification on signup page...
35tu578 signup file updated.

As you can see in the example dev branch is one commit before the HEAD of the master branch.

Now, let’s run the git reset command with the --hard flag option to reset to commit just before the HEAD and also specify the HEAD with command. See the following command example.

When you run the above command it will update the index and make the master branch a HEAD now.

Now, if you run the git log command you will see the master branch as a HEAD.

You successfully changed your feature branch’s HEAD to one commit before HEAD.

If you want to undo a hard reset on Git, use the git reset command with the –-hard option and specify [email protected]{1}. See the following example command.

Conclusion

So in this tutorial, you learn about the git reset command with the flag option like soft, mixed, and hard. You also learnt how you can reset to HEAD your files.

We also learnt the difference between the –soft, –mixed and, –hard flag options with the git reset command and using the examples.

Hope you understand the tutorial, if you have any questions please ask me in the comment section I’ll respond to you as soon as possible.

You May Also Like

About the Author: Aman Mehra

Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+ years of experience. I love coding and help to people with this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *