Git

How to use git reset

The changes of the git repository are done on the current working directory, and the changes can be stage into an index from which the git user can select the next commit. Sometimes the git users need to undo the changes done in the repository for the project purpose. `git reset` command is used to do this task. This command resets the current state of the HEAD to a specific state, and it can work on all commit tasks or an individual file level. Different types of reset mode options can be used with this command. The purpose of each reset option and how some of the reset options can be applied with the `git reset` command for the local git repository have been explained in this tutorial.

Git Reset Options:

Option Purpose
–soft It is used to run the reset command without overwriting any uncommitted modified files. It doesn’t reset the index and doesn’t make changes in the current working tree.
–mixed It is the default reset option, and it resets the index but doesn’t make any changes in the working tree.
–hard It resets both the index and working tree. So, any committed or uncommitted changes made in the working tree will be lost.
–merge It resets the index and modifies the files in the working tree if any difference exists between the index and the working tree. If any file exists in the working directory that is not stashed, then the reset will be aborted.
–keep It resets the index entries and modifies the working tree files any difference between the index and the working tree. If the difference exists in the local file, then the reset will be aborted.
–[no-]recurse-submodules It is used to reset the working tree of all active submodules recursively.

Prerequisites:

Install GitHub Desktop.

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a local repository

You have to create a local repository to test commands used in this tutorial for checking and solving merge conflict.

Git reset for a particular file:

The use of the `git reset` command for a particular file has shown in this part of the tutorial. Run the following command to check the current status of the repository, add the send-email3.php file and reset the send-email3.php file.

$ git status
$ git add send-email3.php
$ git reset send-email3.php

The following output shows that the reset command has changed the tracked file into the untracked file without any option. To change the status of all tracked files of the repository to untracked files, you have to use the ` git reset HEAD ` command.

Git reset with – -soft option:

The use of the `git reset` command with –soft option has shown in this part of the tutorial. This option keeps the working tree unchanged. Run the following command to check the current status of the repository, add the send-email3.php file and reset the repository with the –soft option.

$ git status
$ git add send-email3.php
$ git reset --soft
$ git status

The following output will appear after executing the above commands. The output shows that the working tee has remained unchanged after executing the reset command because the –soft option resets the index only.

Git reset with – -hard option:

The use of the `git reset` command with the –hard option has shown in this part of the tutorial. This option moves the HEAD pointer and updates the content of the working tree with the content where the HEAD is pointing. Run the following command to check the current status of the repository, add the send-email3.php file and reset the repository with the –hard option.

$ git status
$ git add send-email3.php
$ git reset --hard
$ git status

The following output will appear after executing the above commands. The output shows that the working tee has cleaned and nothing to commit after executing the reset command, and the added file has been removed from the repository.

Git reset with –mixed option:

The use of the `git reset` command with –mixed option has shown in this part of the tutorial. This option moves the HEAD pointer and updates the content of the staging area with the content where the HEAD is pointing. But it does not update the working tree-like –hard option, and it provides information about the untracked files. Create a new file named send-email-new.php in the current repository folder. Run the following command to check the current status of the repository, add the send-email3.php file and reset the repository with the –mixed option.

$ git status
$ git add send-email-new.php
$ git reset --mixed
$ git status

The following output will appear after executing the above commands. The output shows that the reset command has kept the current working directory unchanged because the local repository has changed that the task has not been committed. So, the reset task has aborted.

Conclusion:

Four different ways to run the `git reset` command have been explained in this tutorial using a local demo repository. The first reset has been applied to a particular file. The second reset has applied with the –soft option. The third reset has applied with the –hard option. The fourth reset has applied with the –mixed option. I hope the readers will use the `git reset` command properly after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.