On this page
- Which undo command fits your situation?
- How do you discard or unstage edits to files?
- How do reset --soft, --mixed and --hard differ?
- How do you fix the last commit?
- How do you undo a commit that is already pushed?
- How do you park unfinished work with stash?
- How does the reflog rescue lost commits?
- How do you reset a server checkout to a clean state?
- Which mistakes are hardest to undo?
- Questions
Which undo command fits your situation?
Pick the command by where the unwanted change lives and whether anyone else has it yet:
- Edited a file, not staged:
git restore file. - Staged by mistake:
git restore --staged file. - Last commit is wrong, not pushed:
git commit --amendorgit reset. - Several local commits to drop or redo:
git resetto an earlier commit. - Commit already pushed and shared:
git revert. - Need a clean tree for a moment:
git stash. - Something vanished:
git reflog.
Before any of these, read git status and git log --oneline -5 so you know the starting point. If the basic loop is still new, the clone, commit, push and pull guide covers the three areas these commands work on.
How do you discard or unstage edits to files?
git restore, available since Git 2.23, handles both cases with one command and clear flags.
git restore config/app.ini # drop unstaged edits in one file
git restore . # drop all unstaged edits
git restore --staged config/app.ini # unstage, keep the edits
git restore --source=HEAD~2 config/app.ini # take the file from an older commitRestoring the working tree is permanent for those edits, because they were never committed. New files that Git has never tracked are not touched by restore; list them with git clean -n and delete them with git clean -f (add -d for folders). Always run the -n dry run first, since git clean cannot be undone.
How do reset --soft, --mixed and --hard differ?
All three move the current branch to another commit; they differ in what happens to the staging area and your files.
git reset --soft HEAD~1 # undo the commit, keep changes staged
git reset HEAD~1 # same as --mixed: undo commit, keep changes unstaged
git reset --hard HEAD~1 # undo commit AND discard its changes- --soft is perfect for rewriting the last few commits as one: reset, then commit again.
- --mixed (the default) lets you re-stage the changes in smaller pieces with
git add -p. - --hard makes the files match the target commit exactly. Uncommitted work is gone; committed work stays reachable through the reflog.
To make a local branch match the remote again after an experiment, use git fetch and then git reset --hard origin/main. This is a common step on deploy-only clones.
How do you fix the last commit?
If the most recent commit is still local, git commit --amend replaces it with a corrected version.
git add forgotten_file.py
git commit --amend --no-edit # add a file, keep the message
git commit --amend -m "Better message" # change the message only
git commit --amend --reset-author --no-edit # fix a wrong name or emailAmending creates a new commit ID. If you already pushed the original, prefer a follow-up commit, or push with --force-with-lease only on a branch that nobody else uses, as explained in the rebase and cherry-pick guide.
How do you undo a commit that is already pushed?
Use git revert: it writes a new commit applying the exact opposite change, so history stays intact and everyone can pull it normally.
git log --oneline -10
git revert 4c7e2d1 # opens the editor for the message
git revert --no-edit 4c7e2d1 8a90f3b
git revert -m 1 MERGE_COMMIT # revert a whole merge
git pushRevert is the safe choice on main or any branch a server deploys from: production can pull the revert as an ordinary update. If the revert conflicts with later changes, resolve as usual and finish with git revert --continue, or back out with git revert --abort. Reverting a revert brings the original change back later.
How do you park unfinished work with stash?
git stash saves your uncommitted changes on a stack and returns the working tree to the last commit, which lets you switch branches or pull without committing half-done work.
git stash push -m "half done: retry logic"
git stash push -u -m "with new files" # include untracked files
git stash list
git stash show -p stash@{0}
git stash pop # reapply and remove from the stack
git stash apply stash@{1} # reapply, keep it on the stack
git stash drop stash@{1}If pop hits a conflict, the stash is kept so nothing is lost; resolve the files, then drop it yourself. To turn a stash into a proper branch, run git stash branch retry-logic stash@{0}. Stashes are local only, so they never reach the remote; avoid keeping important work there for long.
How does the reflog rescue lost commits?
Every time HEAD moves (commit, reset, rebase, switch), Git writes a line to the reflog. Commits that no branch points at any more can still be found there and restored.
git reflog -15
# e.g. "a1b2c3d HEAD@{3}: commit: Add retry logic"
git branch rescue a1b2c3d # put a branch on it
git reset --hard a1b2c3d # or move the current branch back to itReflog entries are kept for 90 days for reachable commits and 30 days for unreachable ones by default, and they exist only in your own clone. A stash you dropped can also be recovered: git fsck --unreachable | grep commit lists dangling commits, and git show lets you inspect each one.
How do you reset a server checkout to a clean state?
For a deploy directory that should hold exactly what the remote has, fetch and hard reset, then remove stray files.
cd /srv/app
git fetch origin
git reset --hard origin/main
git clean -n -d # preview
git clean -f -d # remove untracked files and foldersExclude data that must survive, such as uploaded files or .env, through .gitignore; git clean without -x leaves ignored files alone. Combine this with daily backups so a mistaken clean on a data folder is never final. A safer, push-driven alternative is shown in the deploy hooks guide.
Which mistakes are hardest to undo?
Three cases deserve extra care because the reflog cannot help:
- Uncommitted edits removed by
git restore,git reset --hardorgit checkout -- file. Commit or stash first when in doubt. - Untracked files deleted by
git clean -f. Always preview with-n. - Secrets pushed to a shared remote. Reverting removes the file from the tip but not from history. Rotate the password or key immediately; scrubbing history needs a tool such as git filter-repo and a coordinated force push.
Questions about Undo with reset, revert and stash
What is the difference between reset and revert?
git reset moves your branch back and rewrites local history. git revert adds a new commit that cancels an old one and is safe on shared branches.
Does git stash save untracked files?
Not by default. Add -u to include untracked files, or -a to include ignored ones as well.
How do I undo a git pull?
Right after the pull, git reset --hard ORIG_HEAD returns the branch to where it was before. Commit or stash local edits first, because the hard reset discards them.
Can I undo git reset --hard?
For committed work, yes: find the earlier position with git reflog and reset back to it. Edits that were never committed are not recoverable from Git.
Is git checkout -- file the same as git restore file?
Yes, both discard unstaged edits to that file. git restore is the newer and clearer spelling.
How do I remove a file from the last commit but keep it on disk?
Run git rm --cached file, add it to .gitignore if needed, then git commit --amend --no-edit while the commit is still unpushed.