Submitting the form below will ensure a prompt response from us.
Git is one of the most widely used version control systems for managing source code and collaborating on software projects. While committing changes is a routine part of development, mistakes can happen—you might commit the wrong files, write an incorrect commit message, or simply realize that the latest commit should be modified or removed.
Fortunately, Git provides multiple ways to undo the last commit depending on whether you want to keep the changes, discard them, or safely reverse a commit that has already been pushed to a remote repository.
In this guide, you’ll learn the most common methods to Undo the Last Git Commit, understand when to use each command, and follow best practices to avoid losing valuable work.
You may need to undo the most recent commit in situations such as:
Choosing the right Git command depends on whether the commit has been pushed and whether you want to preserve the changes.
If you want to remove the commit but keep all your changes in the staging area, use:
git reset --soft HEAD~1
What This Does
This method is useful when you simply want to edit the commit message or add more files before committing again.
If you want to undo the commit but continue editing the files, use:
git reset HEAD~1
or
git reset --mixed HEAD~1
This is one of the most commonly used reset options during development. If you’re not ready to commit your current changes or want to save them before making additional modifications, consider stashing your changes in Git. This lets you temporarily store your work and restore it whenever needed without affecting your commit history.
If the last commit and all associated changes are no longer needed, use:
git reset --hard HEAD~1
What This Does
Use this command only when you’re certain the changes are no longer required.
If you’ve already pushed the commit to a shared repository, avoid using git reset because it rewrites commit history.
Instead, use:
git revert HEAD
What This Does
This is the recommended approach for team environments.
You Might Also Like:
GitHub Actions Environment Variables: How to Use, Manage, and Secure Them
If you only need to update the most recent commit—for example, to fix the commit message or include forgotten files—you can use:
git commit --amend
Example
git add README.md
git commit --amend
This opens your configured text editor, allowing you to modify the commit message before saving the updated commit.
Before removing any commits, review your commit history.
git log --oneline
Example Output
c5a3d8f Update login validation
b8f61d2 Add authentication API
1f84c90 Initial project setup
This helps you verify which commit to undo.
To remove the last three commits while keeping changes staged:
git reset --soft HEAD~3
To completely discard them:
git reset --hard HEAD~3
Always double-check the number of commits before executing these commands.
If you’ve reset a commit that has already been pushed and you intentionally want to overwrite the remote history, you’ll need to force-push.
git push --force
A safer alternative is:
git push --force-with-lease
The –force-with-lease option reduces the risk of accidentally overwriting teammates’ work.
git commit –amend -m “Fix login authentication bug”
git reset –soft HEAD~1
git reset –hard HEAD~1
git revert HEAD
| Command | Removes Commit | Keeps History | Safe After Push |
|---|---|---|---|
| git reset –soft | Yes | No | No |
| git reset –mixed | Yes | No | No |
| git reset –hard | Yes | No | No |
| git revert | No (Creates a reverse commit) | Yes | Yes |
Understanding this difference helps you choose the appropriate command for your workflow.
It preserves commit history and avoids disrupting collaborators.
Use:
git log --oneline
to confirm the commit you intend to undo.
git reset –hard permanently removes uncommitted changes, so use it carefully.
Clear commit messages reduce the need for frequent amendments.
When rewriting Git history, ensure that important work is backed up or communicated to your team.
Incorrect:
git reset --hard HEAD~1
This command permanently removes both the commit and associated changes.
If you want to keep your work, use:
git reset --soft HEAD~1
Avoid running:
git push --force
on shared branches unless absolutely necessary.
Instead, prefer:
git revert HEAD
to maintain a clean and collaborative Git history.
Build Reliable Software with Expert Developers
Our experienced development team helps businesses implement Git workflows, DevOps practices, and scalable software solutions.
Undoing the last Git commit is a common task that every developer encounters during software development. Whether you want to keep your changes, discard them, or safely reverse a commit that has already been pushed, Git provides multiple commands to handle each scenario effectively.
Commands like git reset –soft, git reset –mixed, git reset –hard, and git revert each serve different purposes. Understanding when to use them helps protect your project history, avoid unnecessary conflicts, and maintain a clean, organized Git workflow. By following best practices and reviewing your commit history before making changes, you can confidently manage your repository and recover from mistakes with ease.