How to Undo Mistakes in Git: restore, reset, revert and reflog

Git and CI/CD 3 min readUpdated Oct 29, 2026
3 views0 comments

Everyone makes mistakes in Git: a commit with the wrong message, a file staged by accident, a commit on the wrong branch. Git is very good at recovering from them, if you know which command to use. This guide goes from the safest fixes to the dangerous ones.

Find out where you are first

git status
git log --oneline -5

Always look before you undo anything.

Discard changes in a file (not committed yet)

git restore notes.txt

Warning: this throws away your uncommitted edits to that file. There is no undo for it.

Unstage a file (keep the edits)

git restore --staged notes.txt

The file stays modified in your folder but is no longer part of the next commit.

Fix the last commit

Wrong message:

git commit --amend -m "Correct message"

Forgot a file:

git add forgotten.txt
git commit --amend --no-edit

Amending rewrites the commit. Only do it if you have not pushed it yet.

Undo the last commit but keep the work

git reset --soft HEAD~1    # changes stay staged
git reset HEAD~1           # changes stay in your files, unstaged

Useful when you committed too early or want to split one commit into two.

Undo the last commit and delete the work

git reset --hard HEAD~1

Dangerous: the commit and every change in it disappear from your working folder. Check git status first. (Section on reflog below can still rescue the commit.)

Undo a commit that is already pushed

On a shared branch never rewrite history. Create a new commit that cancels the old one instead:

git revert a1b2c3d
git push

Replace a1b2c3d with the commit id from git log --oneline. Everyone keeps a consistent history, and the mistake stays visible and documented.

You committed on the wrong branch

Say you committed on main but wanted a feature branch. Keep the commit by moving it to a new branch, then put main back:

git switch -c my-feature     # new branch that contains the commit
git switch main
git reset --hard origin/main  # main returns to the remote state

Make sure the commit is on my-feature (git log) before you run the last line.

Set work aside temporarily

git stash
git stash pop

stash saves your uncommitted changes and gives you a clean folder, pop brings them back. Handy when you must switch branches in a hurry.

Rescue “lost” commits with reflog

Git remembers every place HEAD has been for a while, even after a hard reset or a deleted branch:

git reflog

Find the line from before the mistake, for example a1b2c3d HEAD@{3}: commit: Add login page, and bring it back:

git branch rescue a1b2c3d     # safest: new branch pointing at it
git reset --hard a1b2c3d      # or move the current branch there

Reflog entries are kept for weeks by default, so act soon, but you usually have time.

Cheat sheet

Situation Command Safe?
Drop edits to a file git restore file Destroys edits
Unstage git restore --staged file Yes
Fix last commit git commit --amend Before pushing only
Uncommit, keep work git reset --soft HEAD~1 Yes
Uncommit, delete work git reset --hard HEAD~1 Destroys work
Cancel a pushed commit git revert id Yes
Find lost commits git reflog Yes
Did you find this helpful?Tap the heart to let us know. It takes one second and no account.

Leave a Comment

Your email address will not be published. Required fields are marked *

Bigland Network IT

Practical tutorials for people who run, build and fix things: operating systems, databases, development, networking and security.

Topics

More

Site

© 2026 Bigland Network. All rights reserved.Always test commands on a non-production system first.
Scroll to Top