How to Undo the Most Recent Local Commits in Git?
Last Updated :
23 Jul, 2025
When working with Git, it's common to make mistakes or realize that recent commits need to be revised. Whether you've made an error in your code, committed sensitive information, or simply want to restructure your commits, knowing how to undo the most recent local commits is an essential skill. This guide will walk you through the process of undoing your last few commits safely and effectively.
Why Undo Recent Commits?
There are several reasons why you might need to undo recent commits:
- Correcting Mistakes: Fix errors in your code or commit messages.
- Reorganizing Commits: Reorder, squash, or split commits for better clarity.
- Removing Sensitive Information: Eliminate accidental commits of sensitive data.
- Improving Commit History: Maintain a clean and meaningful project history.
There are several methods to undo recent commits which are as follows:
Using git reset
The `git reset` command is powerful and versatile, allowing you to undo commits while preserving or discarding changes in your working directory.
Undo the Most Recent Commit
To undo the most recent commit while keeping the changes in your working directory, use:
git reset --soft HEAD~1
--soft : Keeps your changes staged (in the index).
To undo the most recent commit and discard the changes in your working directory, use:
git reset --hard HEAD~1
--hard : Discards all changes, both staged and unstaged.
Undo Multiple Recent Commits
If you need to undo multiple recent commits, specify the number of commits:
git reset --soft HEAD~3
This example undoes the last three commits, keeping the changes staged.
Using git revert
The `git revert` command is a safer alternative, especially when working with a shared repository, as it creates a new commit that undoes the changes from the specified commit.
Revert the Most Recent Commit
To revert the most recent commit, use:
git revert HEAD
This creates a new commit that undoes the changes of the last commit.
Revert Multiple Recent Commits
To revert multiple commits, specify a range:
git revert HEAD~3..HEAD
This reverts the last three commits, creating new commits that undo the changes.
Using git commit --amend
If you only need to modify the most recent commit message or make minor changes to the commit, use:
git commit --amend
This opens your default text editor, allowing you to modify the commit message. If you want to add or remove files from the commit, stage the changes before running the `--amend` command.
Example 1: To undo the Most Recent Commit and Keep Changes
git reset --soft HEAD~1
This command undoes the most recent commit, keeping the changes staged.
Example 2: To undo the Most Recent Commit and Discard Changes
git reset --hard HEAD~1
This command undoes the most recent commit and discards all changes.
Example 3: Revert the Most Recent Commit
git revert HEAD
This command creates a new commit that undoes the changes of the most recent commit.
Example 4: Amend the Most Recent Commit Message
git commit --amend
This command allows you to edit the most recent commit message.
Explore
Git Introduction
Git Installation and Setup
All Git Commands
Most Used Git Commands
Git Branch
Git Merge
Git Tools and Integration
Git Remote Repositories
Collaborating with Git
Advanced Git Commands