4) Undoing Changes in Git Lesson

Git Reset

18 min to complete · By CodingNomads Team

Git Reset HEAD

The git reset command allows you to undo mistakes you made during development by going back to a previous version of your project. Being able to change your mistakes or undo unwanted changes is one of the most helpful and attractive features of Git and version control.

Since Git stores a snapshot of the project with each commit that you make, you can revisit all saved older versions of your project whenever needed.

This lesson discusses git reset, git reset --hard, git reset --mixed, and git reset --soft`.

Git Reset HEAD Options

The git reset command is a bit more complicated than other undo commands. The command does different operations depending on what option you pass to it. It can do three different actions, depending on which option you choose:

  • git reset --soft: Resets the current state of HEAD into a specified state only
  • git reset --mixed: Resets the current state of HEAD into a specified state and clears the staging area
  • git reset --hard: Resets the current state of HEAD into a specified state, clears the staging area, and resets the file system as well

These three options work with the three internal states of Git's structure:

  • Commit History (HEAD)
  • Staging Index
  • Working Directory

Git uses these three locations both for creating as well as for reverting commits. The different options affect different locations of this process, as visualized in the following graphic:

Git Reset Options Schema

Let's learn about each of the three options for git reset in more detail, beginning with the most destructive one of the three.

Git Reset Hard

The git reset --hard flag is the most direct and unsafe option among the three. Despite this, it is frequently used by developers.

When you run git reset --hard, it will update your project's commit history to point to the specified commit. It will also reset the staging area and your working directory to match the specified commit. This means that any pending work that was in the staging area and in the working directory will be lost.

Colorful illustration of a light bulb

Note: git reset --hard resets the HEAD, the index, and the working directory to match a specified commit

Let's spell this out again, since it is important that you are aware of what will happen if you run this command:

  1. The HEAD pointer is moved to the specific commit reference
  2. The staging area will be emptied
  3. The working directory will be changed to match the state of your project during the specified commit

You can see that this powerful command could have effects that you might regret later on. Conversely, it can often come in handy if you want a fresh start. Let's take a look at what this process may look like.

Colorful illustration of a light bulb

Note: The screenshots below show a random Git repository with commits to exemplify the points. Feel free to try out running the same commands on one of your example Git repositories, e.g., the one you have been working with in the previous lesson.

Git Reset Hard Example

On the following example repository, let's reset to a specific commit using the --hard flag to change everything:

Start by running git log to check the current commit history:

Git Log before running Reset Hard

To reset the project to a specific commit, you run git reset --hard <commit hash>. In this example, the command looks like this:

$ git reset --hard 1bf9c9a

You will see that the current position of the HEAD now points to the specified commit state:

Git Reset Hard Output

Let's run the git log command again to see the changed state of your repo after the reset command took action:

Git Log output after running Git Reset Hard

Looking at the screenshot above, you can see that now the HEAD is pointing at the commit you specified in your git reset command. You will also notice that your working tree has also been reset to that previous state.

Git Reset Hard Practice

It often helps to give a concept a try on your own machine. Here's how to run a hard reset with the git reset --hard command:

  1. Create a new file called NewFile.txt in your existing repository
  2. Add the file with git add. Don't commit the file yet.
  3. Check the status of your repo with the git status command
  4. Run git log to check the status of the HEAD and previous commits. As you haven't committed the NewFile.txt yet, the current HEAD position of the repository still remains the same
  5. Run the git reset command like so:
$ git reset --hard

The git reset --hard resets all the changes and matches the position of the HEAD to before the last changes were made. It will also remove anything from the staging area, which means that your file will be gone from the file system and the staging area.

Let's investigate with this checklist:

  • Run git status to inspect your repository after running git reset --hard. Git will tell you that you have nothing to commit and that the repository is up to date.
  • All changes have been removed by the --hard option.
  • Your current HEAD matches the status of the previous one.
  • The file NewFile.txt has been completely removed from your repository.

Git Reset Mixed

The git reset --mixed flag is the default option of the git reset command. If you don't pass any argument to the command, then git reset executes as --mixed.

This version of the command updates the reference pointers and resets the staging area. However, it doesn't affect your working directory. Therefore, you can think of it as though the undone changes are transferred to the working directory.

To sum it up, the --mixed option will have the following effects:

  • The HEAD pointer is moved to the specific commit reference
  • The staging area will be emptied
  • The working directory will remain the same

Git Reset Mixed Example

Let's look at another example of this with the example repo you saw earlier in this lesson:

Assume you want to reset to a previous commit and clear your staging area, but leave the working directory untouched. You go ahead and run:

$ git reset --mixed a22da94

After running the git log command to see the state of the repository, you'll see something like this:

Git Log after Git Reset Mixed

In the screenshot above, you can see that your current HEAD points towards the specified commit.

If you run git status to check the status of your repository after running the reset command, you'll see that there are now some untracked files. The staging area is empty, but the working directory retained the state it had before running the command:

Git Status after Git Reset Mixed

Therefore, new or changed files as compared to the checked-out HEAD state of the project will show up. The default --mixed mode doesn't remove files from the working tree like the --hard mode does.

Git Reset Soft

The git reset --soft mode doesn't change the staging area nor the working directory; it only moves the HEAD pointer to the specified commit. When you run reset in the --soft mode, the reference pointers are updated, then the reset stops.

Git Reset Soft Example

Let's look at the git reset --soft command through the example repo from before:

Using git log, the commit history of the example repository looks like this:

Git Log before soft reset

Again, you could run the reset command to point to a specific commit:

$ git reset --soft 5dee0f3

The Git history will point to a different commit. Running git log now shows the following:

Git log after running soft reset

You can see that the HEAD is now pointing to the previously specified commit. Next, let's check up on the rest of the state of this repository by running the git status command:

Git status after soft reset

You can see a list of files on the screenshot. These files have all been created since the current commit and the previous HEAD pointer, which indicates the furthest progress of the project.

You can also see that they are already in the staging area. You could add all of them back to your Git history in a single commit by running git commit:

$ git commit -m "files added again"

After that, running git log will show you a changed history on the Git repository:

Git log after soft reset

As you can see, the last commit now shows up there as the new HEAD.

Git Reset HEAD Reference Guide

Going over all three options, you now have an idea about how the different options of git reset work to undo changes, and you're better equipped to choose the right one for your situation. Here is a table you can use as a quick reference:

Reset Mode Head pointer Staging Index Working Tree
git reset -- soft Moves Unchanged Unchanged
git reset -- mixed (default) Moves Changes Unchanged
git reset -- hard Moves Changes Changes

Keep in mind that you can always access Git's manual for any specific command that you want to learn more about with the man command:

$ man git reset

This will show you a whole lot of information about the git reset command right in your CLI:

Git Reset Man Page

If you want to dive deeper, check out the Git Reset and Git Tools - Reset Demystified resources.

Summary: Git Reset HEAD

The git reset command allows you to undo mistakes you made during development by going back to a previous version of your project. The command executes different operations depending on what option you pass to it:

  1. git reset --soft resets the current state of HEAD into a specified state only.
  2. git reset --mixed resets the current state of HEAD into a specified state and clears the staging area.
  3. git reset --hard resets the current state of HEAD into a specified state, clears the staging area, and resets the file system as well.