Undoing Mistakes
How you undo a mistake in Git depends on whether you edited a file, created a local commit, or already pushed your changes to a remote repository.
Choosing the right command helps you correct mistakes while avoiding accidental loss of work or disruption to your team's shared history.
git restore and
git reset --hard can discard work.
Check your Git status and save anything you need
before running destructive commands.
1. You Edited a File but Haven't Committed It
If you want to discard changes to a tracked file and restore it to the version in your last commit, use the following command.
git checkout -- filename.ext
In newer Git versions, you can use
git restore instead.
git restore filename.ext
2. You Committed Locally but Want to Undo the Commit
If your latest commit has not been pushed and you
need to undo it, git reset provides
different options depending on whether you want
to keep your changes.
Keep Your Changes
git reset --soft HEAD~1
Removes the latest commit from the current branch while keeping its changes staged.
Discard the Commit and Its Changes
git reset --hard HEAD~1
Moves the current branch back one commit and resets tracked files to that commit. Changes introduced by the removed commit may be lost from your working directory.
git reset --hard can permanently
discard uncommitted tracked-file changes.
Prefer --soft when you want to
preserve your work.
3. You Forgot to Include a File in Your Last Commit
If you made a commit but forgot to include a file, you can stage the missing file and amend the previous commit.
git add forgotten-file.js
git commit --amend --no-edit
The --amend option updates the latest
commit, and --no-edit keeps its
existing commit message.
4. You Already Pushed the Bad Commit to GitHub
If the commit is already on a shared remote
repository, git revert is often
the safer choice. It creates a new commit that
reverses the changes introduced by an earlier commit.
Step 1: Find the Commit
git log --oneline
Step 2: Revert the Commit
git revert <commit-hash>
Replace <commit-hash> with the
actual commit ID you want to reverse. Git may
open an editor for the revert commit message.
Step 3: Push the Revert Commit
git push origin <branch-name>
Publishes the new revert commit to the remote branch, assuming you have the required permissions.
Quick Reference
| Situation | Command | Result |
|---|---|---|
| Discard changes in one file | git restore filename.ext |
Restores the tracked file's working-tree version. |
| Undo latest commit, keep changes staged | git reset --soft HEAD~1 |
Removes the commit but keeps its changes staged. |
| Undo latest commit and discard tracked changes | git reset --hard HEAD~1 |
Moves the branch back and resets tracked files. |
| Update the latest commit | git commit --amend --no-edit |
Amends the commit while keeping its message. |
| Undo a commit already pushed | git revert <commit-hash> |
Creates a new commit that reverses the selected commit. |
git restore for file changes,
git reset for local commit history,
git commit --amend to correct the
latest commit, and git revert to
safely reverse a commit already shared with others.