How to uncommit my last commit in Git
Categories:
How to Uncommit Your Last Commit in Git

Learn various methods to undo your last Git commit, from soft resets to hard resets, and understand when to use each approach to manage your repository history effectively.
Accidentally committed something you shouldn't have? Made a mistake in your last commit message or included the wrong files? Don't worry, Git provides powerful tools to undo your last commit. This article will guide you through different scenarios and the appropriate Git commands to revert your changes, ensuring your repository history remains clean and accurate.
Understanding Git Reset
The primary command for undoing commits in Git is git reset
. This command is incredibly versatile and can modify your commit history, staging area (index), and working directory in different ways, depending on the mode you choose. It's crucial to understand the implications of each mode before using them, especially in shared repositories.
flowchart TD A[Start: Last Commit] --> B{Choose Reset Mode?} B -->|--soft| C[HEAD moves, Index & Working Dir unchanged] B -->|--mixed (default)| D[HEAD moves, Index reset, Working Dir unchanged] B -->|--hard| E[HEAD moves, Index & Working Dir reset] C --> F[Changes staged, ready to recommit] D --> G[Changes unstaged, in working directory] E --> H[Changes discarded, working directory clean] F --> I[End: Ready for new commit] G --> I H --> I
Flowchart illustrating the effects of different git reset
modes.
Method 1: Soft Reset (--soft
)
The --soft
option for git reset
is the safest way to uncommit your last commit. It moves the HEAD
pointer to the previous commit, but it keeps all the changes from the uncommitted commit in your staging area (index). This means the files are still marked as 'staged for commit', allowing you to easily amend the commit or create a new one with the same changes.
git reset --soft HEAD~1
Performing a soft reset to uncommit the last commit.
HEAD~1
to refer to the commit directly before the current HEAD
. You can also use the commit hash of the commit you want to reset to.Method 2: Mixed Reset (Default - --mixed
)
The --mixed
option is the default behavior of git reset
if no mode is specified. Like --soft
, it moves the HEAD
pointer to the previous commit. However, it also unstages the changes from the uncommitted commit, moving them back to your working directory. This means the files are modified but not staged, giving you a chance to review and selectively stage them again.
git reset HEAD~1
# Or explicitly:
git reset --mixed HEAD~1
Performing a mixed reset to uncommit and unstage changes.
Method 3: Hard Reset (--hard
)
The --hard
option is the most destructive form of git reset
. It moves the HEAD
pointer to the previous commit, and then it discards all changes from the uncommitted commit from both your staging area and your working directory. This means any changes made in that commit are permanently lost from your local repository. Use this with extreme caution, especially if you haven't pushed your changes yet.
git reset --hard HEAD~1
Performing a hard reset to completely discard the last commit and its changes.
git push --force
), which is generally discouraged in shared branches.What if the commit has already been pushed?
If you've already pushed your commit to a remote repository, using git reset
(especially --hard
) can cause issues for collaborators. In such cases, git revert
is generally preferred. git revert
creates a new commit that undoes the changes introduced by a previous commit, preserving the history. This is a non-destructive way to undo changes in a shared history.
git revert HEAD
Using git revert
to create a new commit that undoes the last commit.
git revert
, Git will open your default editor to allow you to modify the revert commit message. Save and close the editor to complete the revert.