Undo Changes – git reset

Resetting Changes with git reset

git reset is the command used when you want to reset INDEX or change commit histories with or without changing the files in the Working Tree.

There are mainly two ways you can use this command. We'll explain them using the illustration below. The illustration describes the original status of commits, INDEX, and the Working Tree before running the git reset command.

git reset: original status

No commit hash and option

Clear files in INDEX (Staging Area). To clear files in INDEX, you need to simply run the git reset command like the one below. For this purpose, there is no need to use any option and commit hash.

git reset
git reset (no commit hash and option)

With a commit hash and option (soft, mixed, hard)

To change the commit history, you need to specify which commit to go back to by using a commit hash. With this command, the commits made after the commit hash will be erased from the history. As the command can create a significant change, you need to carefully run the command.

When changing commit histories, there are three options depending on how you want to change the status of INDEX and the Working Tree.

"--soft" option

With this option, INDEX and Working Tree stay unchanged. Only commit histories are reversed as described in the illustration below.

git reset --soft

If you want to move back to commit hash 1234567 with this option, run the command below.

git reset --soft 1234567

"--mixed" option (default)

With this option, only the INDEX status will be back to the same status as the commit you designated for this command, as described in the illustration below. The Working Tree stays the same. As --mixed is the default option, if you don't add an option when running the command, the command will act like with the --mixed option.

git reset --mixed

If you want to move back to commit hash 1234567 with this option, run the command below.

git reset --mixed 1234567

"--hard" option (default)

With this option, the statuses of INDEX and the Working Tree will be back to the same status as the commit you specified for this command as described in the illustration below. You need to be extremely careful to run the command with this option as all the changes made after the specified commit will be completely erased from the three areas (all commits, INDEX, and the Working Tree).

git reset --hard

If you want to move back to commit hash 1234567 with this option, run the command below.

git reset --hard 1234567

Practice

bloovee-round-icon.pngDeveloper A (Project Owner Role)

Objective:
Practice the git reset command with different options

1. Practice preparation

In this practice, we'll use the same example as in the git diff practice explained earlier. Please review the practice section of the git diff page if you haven't gone through it yet. The practice file named git_practice.html has the commit history, INDEX, and the Working Tree illustrated below.

git reset: default settings for the practice

When you execute these steps on your computer, you see different commit hashes. For your practice, use the commit hash generated on your computer.

2. git reset (clear INDEX)

Before running the git reset command, check the current status of INDEX by running the git diff command.

Command Line - INPUT
git diff

You'll see that the color status of the Working Tree is red while the color status of INDEX is yellow. (git diff shows the changes in the Working Tree compared to INDEX:

  • After + shows the status of the Working Tree
  • After - shows the status of INDEX
Command Line - RESPONSE
-  color: yellow;
+  color: red;

To clear the files in INDEX, you can simply run the git reset command. To check the changed status, run the git diff command.

Command Line - INPUT
git reset
git diff

You can see that the color status in INDEX is now green, which is the same as the HEAD.

Command Line - RESPONSE
-  color: green;
+  color: red;

For the next exercise, reverse INDEX and the Working Tree to the original setting with the following steps.

  • Update the color status in the Working Tree to yellow and save the file.
  • Run git add git_practice.html.
  • Update the color status in the Working Tree to red and save the file.

To confirm the status is back to the original, run the git diff command.

Command Line - INPUT
git diff

If you see that the color status is the ones below, the color of INDEX is back to yellow, and the Working Tree's color is red.

Command Line - RESPONSE
-  color: yellow;
+  color: red;

Once you confirm that the status is back to the same as the practice default settings, go to the next exercise.

3. git reset --soft

git reset with the --soft option is used to change only commit histories. In this practice, we'll target going back to "The first commit", whose commit hash is 651e510. On your computer, you see a different hash. You need to use the commit hash generated on your computer.

To execute the command and confirm the results, run the git reset --soft command followed by git log.

Command Line - INPUT
git reset --soft 651e510
git log --oneline

In the command line response, you see that "Modified commit" disappears and HEAD becomes "The first commit" (651e510) like below.

Command Line - RESPONSE
651e510 (HEAD -> master) The first commit

Also, check the status of INDEX and the Working Tree by running the git diff command.

Command Line - INPUT
git diff

You'll see that INDEX and the Working Tree remain the same.

  • INDEX -> yellow ( after the - mark)
  • Working Tree -> red ( after the + mark)
Command Line - RESPONSE
-  color: yellow;
+  color: red;

For the next exercise, reverse the commit history to the original status. When reversing the git reset operation, you can use ORIG_HEAD.

IdeaTips: ORIG_HEAD

ORIG_HEAD is the commit before the git reset command is executed. This is useful for reversing the operation you made using the git reset command

Run the following commands and see that the commit history is back to its original state.

Command Line - INPUT
git reset ORIG_HEAD
git log --oneline
Command Line - RESPONSE
446fa13 (HEAD -> master) Modified commit
acc4aa4 (origin/master) added .gitignore file
651e510 the first commit

4. git reset --mixed

The --mixed option is the default setting. You can run the command without the option to get the same results. You can try the command both with the option and without the option. You need to use the commit hash generated on your computer.

Command Line - INPUT
git reset --mixed 651e510

Or

git reset 651e510

To check the results, run the git log and git diff commands.

Command Line - INPUT
 git log --oneline
Command Line - RESPONSE
651e510 (HEAD -> master) the first commit

You can see that the HEAD is now The first commit (651e510)

Command Line - INPUT
git diff
Command Line - RESPONSE
-  color: blue;
+  color: red;

You can see that INDEX becomes the same as the HEAD while the Working Tree remains the same.

  • INDEX -> blue ( after the - mark). This is the same as the HEAD.
  • Working Tree -> red ( after the + mark)

For the next exercise, do the following.

  1. Reverse the commit histories to the original status by running the git reset ORIG_HEAD command.
  2. Reverse the INDEX status with the following steps.
    • Update the color status in the Working Tree to yellow and save the file.
    • Run git add git_practice.html and save the file.
    • Update the color status in the Working Tree to red and save the file.

Once you confirm that the status is back to the same as the practice default settings, go to the next exercise.

5. git reset --hard

The --hard option is the strongest option. The command will clear the current Working Tree and INDEX status, and make them the same as the target commit status. As your changes can be completely erased by this command, you need to be very careful when you are running the command with this option.

By running the following commands, you can see the same results as in the case of the other options. You need to use the commit hash generated on your computer.

Command Line - INPUT
git reset --hard 651e510
git log --oneline
Command Line - RESPONSE
651e510 (HEAD -> master) the first commit

To check the status of INDEX, run the git diff or git diff --cached command. For both commands, you don't get any response as HEAD, INDEX, and the Working Tree reach the same status after the git reset --hard command.

To check the difference between INDEX and the Working Tree, run the command below. There will be no response.

Command Line - INPUT
git diff

To check the difference between INDEX and HEAD, run the command below. There will be no response.

Command Line - INPUT
git diff --cached