Undo Changes – 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.
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
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.
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.
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).
If you want to move back to commit hash 1234567
with this option, run the command below.
git reset --hard 1234567
Practice
Developer 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.
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.
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
- 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.
git reset
git diff
You can see that the color status in INDEX is now green
, which is the same as the HEAD.
- 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.
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
.
- 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
.
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.
651e510 (HEAD -> master) The first commit
Also, check the status of INDEX and the Working Tree by running the git diff
command.
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)
- 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
.
Tips: 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.
git reset ORIG_HEAD
git log --oneline
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.
git reset --mixed 651e510
Or
git reset 651e510
To check the results, run the git log
and git diff
commands.
git log --oneline
651e510 (HEAD -> master) the first commit
You can see that the HEAD is now The first commit (651e510
)
git diff
- 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.
- Reverse the commit histories to the original status by running the
git reset ORIG_HEAD
command. - 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.
- Update the color status in the Working Tree to
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.
git reset --hard 651e510
git log --oneline
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.
git diff
To check the difference between INDEX and HEAD, run the command below. There will be no response.
git diff --cached