Depending on the outcome you want to have, you can use either the git reset
or git revert
command to undo git commit(s).
This tutorial shows you how to undo a commit using both commands. You will also learn when to use them.
Undo git commit with reset
The git reset
command is used to undo previous commit(s) from your commit history.
Let’s see an example of how git reset
works. Suppose you have a Git repository with commit history as shown below:
A->B->C->(U)
^
HEAD
Currently, your head is in commit C, and there are some unstaged changes (U)
in your local repository.
Say you want to remove git commit C, here’s how you do it with git reset
:
git reset head~1
The above command will undo the last commit, so your commit history will look as follows:
A->B---->(U)
^
HEAD
The commit C is removed, but the changes won’t be deleted. You can edit those changes and commit them again.
If you want to remove the commit and the changes, you need to add the --hard
option to the reset command:
git reset --hard head~1
By using hard reset, both the last commit and all the changes you have will be removed like this:
A->B
^
HEAD
And that’s how the git reset
command works.
The head~1
commit parameter is used to identify the most recent commit in your history.
If you want to undo multiple commits, then you can adjust the commit parameter accordingly.
# undo 2 latest commit
git reset head~2
# undo 3 latest commit
git reset head~3
Alternatively, you can pass the commit hash you found when running the git log
command to reset to a specific commit.
Suppose you have a commit history as follows:
$ git log
commit 83f371450dea8c71812fca93b79d9427306a438b (HEAD -> main)
Author: Nathan Sebhastian
Date: Mon Dec 19 19:33:28 2022 +0700
3
commit 1a98501637c672fcc15f3d7ed4bbb1c1596f950a
Author: Nathan Sebhastian
Date: Mon Dec 19 19:26:29 2022 +0700
2
commit 1f1d6c731129b2a57e368a7c93f6599c39653d46
Author: Nathan Sebhastian
Date: Mon Dec 19 19:26:10 2022 +0700
init
To undo to the earliest commit, you can pass the first seven characters of the commit hash like this:
git reset 1f1d6c7
And that’s how you undo commits using the git reset
command.
Next, let’s see how the git revert
command works.
Undo git commit with revert
The git revert
command is used to reverse change from previous commits by creating a new commit.
This command is considered safer than git reset
because it doesn’t modify your commit history.
Let’s see an example of using git revert
. Suppose you have a commit history as follows:
A->B->C
^
HEAD
Now let’s say commit C was a mistake that you want to undo. You can use git revert
to create a new commit that undo the changes recorded in commit C:
git revert <commit hash>
The git revert
command will ask you to write a description for a new commit.
Once done, you’ll see a new commit recorded in your history:
A->B->C->(Revert C)
^
HEAD
As you can see, using the git revert
command adds a new commit that reverses the change introduced by a previous commit.
Keep in mind that when you want to undo multiple commits, you need to use git revert
to undo each commit in the right order.
For example, to revert commits B and C, you need to run the command like this:
$ git revert --no-commit C
$ git revert --no-commit B
$ git commit -m "Revert commit C and B"
Now that you’ve learned how git revert
works, let’s learn when you should use reset or revert command.
When to use reset or revert?
The git reset
command is recommended when you have not pushed your commit to the remote repository.
This is because git reset
effectively modifies the local commit history, so it might overwrite history that’s required by other people.
Once you have pushed the commit to your remote repository, it’s recommended to use git revert
because it creates a new commit that undo the changes done in the previous commit.
But if you commit a security mistake such as committing your .env
file with sensitive information, then you definitely want to use git reset
instead of git revert
.
If you are a solo developer, then you are also free to use git reset
all the time.
Now you’ve learned how to undo a Git commit. Well done!