You have just made a Git commit and realised you were not quite ready. Maybe you included two unrelated changes, missed a file or want to review the code again.
You want to undo the commit, but you still need the work in your files.
For a local commit you have not pushed, git reset --soft HEAD~1 moves your branch back one commit while keeping your changes staged. If you want to keep the changes but unstage them, use git reset --mixed HEAD~1.
Before choosing a command, check whether the commit has already been shared. Undoing a local commit and reversing a shared commit are different tasks.
First, check where you are
Open a terminal in your repository and run:
git status
git log --oneline -3
git status shows your current branch and whether you have staged, unstaged or untracked files.
git log --oneline -3 shows the three most recent commits, with a short identifier and message for each.
For example:
b82f6a1 Update contact page heading
a14c390 Add contact page
62de108 Create project
The top entry is the latest commit. In this example, that is the one we want to undo.
The examples below assume:
- You are on the branch you intend to change.
- The latest commit is an ordinary commit, not a merge commit.
- There is an earlier commit to move back to.
- Your working tree is clean before you begin.
A clean working tree means there are no pending file changes. If you have made more edits since committing, save those separately before following the example. Keeping the starting point simple makes the result easier to understand.
What do staged and unstaged mean?
Before undoing anything, it helps to understand where Git keeps track of your work.
Your working files are the files you edit in your project.
Staged changes are changes selected for the next commit, usually using git add.
A commit records a snapshot of the staged content in your repository’s history.
Undoing a commit does not have to mean deleting the edits. You can move the branch back and keep those edits available to review or commit again.
Option 1: Undo the commit and keep the changes staged
If you have not pushed the commit and want its changes ready to commit again, run:
git reset --soft HEAD~1
Here is what the command means:
git resetmoves the current branch to a specified commit.--softleaves the staging area and working files unchanged.HEAD~1identifies the parent of the current commit: one commit back in this example.
HEAD normally points to the commit you currently have checked out.
Now check the result:
git status
git log --oneline -3
The latest commit should no longer be at the tip of your branch. Its file changes should appear under Changes to be committed.
For our example, the status could include:
Changes to be committed:
modified: contact.html
Open contact.html. The updated heading should still be there.
You have undone the commit while keeping its changes staged.
Review and commit again
To inspect what is staged, run:
git diff --cached
If the staged changes are ready, create a new commit:
git commit -m "Improve contact page heading"
If you make more edits before committing, stage those edits too. A soft reset keeps the previous staged content, but it does not automatically stage anything you change afterwards.
git add contact.html
git diff --cached
git commit -m "Improve contact page heading"
Option 2: Undo the commit and unstage the changes
If you want to review the files and choose what to stage again, use:
git reset --mixed HEAD~1
This moves the branch back one commit and resets the staging area to that earlier commit. Your working files stay as they are.
For a file modified by the undone commit, git status could show:
Changes not staged for commit:
modified: contact.html
The heading edit is still in the file, but it is no longer selected for the next commit.
If the undone commit added a new file, that file will normally appear as untracked instead. It still exists on your computer; Git is no longer tracking it at the earlier commit.
You can review changes to tracked files with:
git diff
Open any untracked files separately, because a normal git diff does not show their contents.
When ready, stage the files you want and commit:
git add contact.html
git diff --cached
git commit -m "Improve contact page heading"
--mixed is the default reset mode, so git reset HEAD~1 has the same effect. I have written the option explicitly here to make the difference easier to follow.
Choose one reset option
Use --soft when you want to undo the local commit and leave its changes staged.
Use --mixed when you want to undo the local commit and select the changes for your next commit again.
These are alternatives. Do not run both commands one after another to undo the same commit. Each command moves the branch back from its current position, so running both would move it back twice.
What if you already pushed the commit?
If other people may have pulled the commit, a reset followed by a force push can disrupt their work by rewriting shared history.
When you need to reverse the effect of a shared commit, git revert is usually the better starting point:
git revert --no-edit HEAD
This creates a new commit that reverses the latest commit’s changes. The original commit stays in the history.
--no-edit accepts Git’s generated commit message without opening an editor.
Check the result and run any relevant project checks before pushing:
git status
git log --oneline -3
git push
Use your team’s normal branch and pull request process if direct pushes are not allowed.
Revert has a different goal from reset: it reverses the changes in your files and records that reversal. It does not leave those changes in place for editing.
If you already pushed but want to keep the code and only tidy the commit history, pause before changing it. Agree on the approach with anyone using the branch.
What if revert reports a conflict?
A conflict means Git needs help deciding how to apply the reversal. Read the terminal output and inspect the affected files.
If you decide not to continue with an in-progress revert, you can cancel it with:
git revert --abort
Merge commits also need special handling. The simple revert command above is intended for an ordinary commit.
What if only the commit message is wrong?
For a local commit you have not shared, you can change the message without resetting:
git commit --amend -m "Improve contact page heading"
Check git status first. Amending also includes any currently staged changes, so make sure you are not accidentally adding more code when you only intended to change the message.
Amending creates a replacement commit with a new identifier. Treat an already shared commit differently rather than amending and force-pushing without checking.
Avoid --hard when you want to keep your work
--hard changes the working files as well as the staging area. It can discard tracked changes.
It is not the option to choose for the task in this article: undoing a commit while keeping the edits.
Read the full command before running it, including when it comes from an AI assistant or a copied answer. A small difference in a Git command can have a very different effect.
Check the files, not just the history
After undoing a local commit, check three things:
git logshows the branch at the expected commit.git statusshows whether your changes are staged, unstaged or untracked.- Your files still contain the work you wanted to keep.
Once those match your intention, you can make the remaining edits and commit again. Understanding where the changes went is more useful than remembering a command on its own.
Further reading:
No comments