How to Undo Last Git Commit

Sometimes, when working with Git, you may want to undo the latest commit. A commit is a snapshot of a Git repository at a given time. Git has a reference variable called HEAD that points to the latest commit in the current working branch. To undo a commit, all you need to do is point the HEAD variable to the previous snapshot.
This guide explains how to undo the last Git commit.
Undoing a commit that has already been pushed to a shared repository is not recommended. If the commit is already pushed, use git revert instead, as shown in the Undoing a Pushed Commit
section below. If you only want to change the commit message, check out this article
.
Git Three-Tree Architecture
In Git you can undo changes using the git reset command followed by the commit identifier.
git reset takes additional arguments that allow you to control the command behavior. To better understand how reset works, let us talk about Git’s three different trees. The three-tree architecture is a key concept of the Git management system. They are called trees because they represent collections of files.
Git manages and manipulates the following three trees:
- The Working Directory - A directory, including all subdirectories and files on the local filesystem that is associated with the repository. It is often referred to as a “working tree”. The working directory is something like a sandbox where you can test the changes before committing them to the staging index.
- The Index - This tree keeps track of new or changed files that have been added to the index with
git add, to be included in the next commit. It is often referred to as “staging area” or “staging index”. - The
HEAD- A pointer to your last commit on the current branch.
The git reset command has three arguments that correspond to the three trees:
--soft- Updates theHEADpointer to the given commit. The Working directory and Index are not changed.--mixed- Updates theHEADpointer and resets the Index to the specified commit. The Working directory is left untouched. This is the default operation mode of theresetcommand.--hard- Updates theHEADpointer and resets the Index and the Working directory to the specified commit. Be extra careful when using this option, as all local changes you have not committed will be overwritten and lost.
Undoing the Last Commit
To undo the last commit without losing the changes you made to the local files and the Index, invoke git reset with the --soft option followed by HEAD~1:
git reset --soft HEAD~1HEAD~1 is a variable that points to the previous commit. The command above moves the current branch backward by one commit, effectively undoing your last commit. If you run the git status command, you will see that the changed files are listed as uncommitted changes.
To update the HEAD pointer to reset the Index, run git reset with --mixed or without an option:
git reset --mixed HEAD~1
git reset HEAD~1The changed files are kept, but unlike the previous example, the changes are now not staged for commit.
If you do not want to keep the changes you made to the files, invoke the git reset command with the --hard option:
git reset --hard HEAD~1Before performing a hard reset, make sure you do not need the changes anymore.
Undoing Multiple Commits
With git reset, you can return to any previous commit.
For example, to move the current branch back three commits, you would use:
git reset --hard HEAD~3Since we are using --hard, the command above will remove the latest three snapshots from the commit history.
Another way to move back to a specific commit is to pass the commit ID to the git reset command.
Use git log --oneline
to find the commit IDs:
git log --onelineThe command will display a list of all commits, including the ID and the first line of the commit message:
32921222 (HEAD -> master) Update changelog
7505724c adding new tests
750862ce new blog post
95a63417 sort configuration file
252032e4 Refactor User class
...Once you know the ID of the commit you want to reset to, just pass the ID to the git reset command:
git reset --hard 95a63417Undoing a Pushed Commit
The git reset command rewrites the commit history, which is fine while the commit lives only on your local machine. Once a commit is pushed to a shared repository, resetting it changes history that other people may already have, which leads to conflicts on their next pull.
To undo a commit that has already been pushed, use the git revert
command instead. Rather than deleting the commit, revert creates a new commit that reverses the changes introduced by the target commit, so the existing history stays intact.
To revert the most recent commit, pass HEAD to the command:
git revert HEADGit opens your default text editor with a prefilled commit message describing the revert. Save and close the editor to create the new commit. You can then push it like any other commit, and the unwanted changes are removed without rewriting the branch history.
Quick Reference
For a printable quick reference, see the Git cheatsheet .
The table below summarizes the commands covered in this guide and what each one does to your changes:
| Command | Effect |
|---|---|
git reset --soft HEAD~1 | Undo the last commit, keep the changes staged |
git reset --mixed HEAD~1 | Undo the last commit, keep the changes unstaged (default) |
git reset --hard HEAD~1 | Undo the last commit, discard the changes |
git reset --hard HEAD~3 | Undo the last three commits, discard the changes |
git revert HEAD | Reverse the last commit with a new commit (safe after a push) |
Conclusion
To undo the last commit, use the git reset command and choose the option that matches whether you want to keep or discard your changes. If the commit is already pushed, reach for git revert instead so you do not rewrite history that your colleagues depend on.
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page