To discard local changes in Git and go back to the last commit, run git restore <file> for one file or git restore . for all of them. On Git before 2.23, use git checkout -- <file> instead. To delete files Git is not yet tracking, you need git clean -fd. None of this is recoverable once it runs, so be sure before you press enter.
That is the whole answer for uncommitted edits. Further down, the same page covers the rest of the undo toolkit: undoing the last commit, choosing reset or revert, recovering lost commits with the reflog, and removing a file from Git without deleting it.
What "local changes" actually means
When you edit files in a repository, Git sorts them into states. Knowing the state tells you which command to reach for. If the difference between these is fuzzy, the staging area walkthrough is worth ten minutes first.
| State | What it means | How to discard it |
|---|---|---|
| Modified, tracked, unstaged | You edited a file Git already knows about, no git add yet | git restore <file> |
| Modified, tracked, staged | Same, but you ran git add on it | git restore --staged <file> then git restore <file> |
| Untracked | A new file Git has never seen (no git add ever) | git clean -f |
| Everything | Throw away the whole working tree | git reset --hard plus git clean -fd |
Run git status first, every time. It tells you exactly which bucket each file is in, and it spells out the discard command for that bucket right in its output. Reading status before discarding is the single habit that prevents most "I lost my work" moments.
Discard changes to a tracked file
This is the common case: you changed a file, the change is wrong, and you want the file back the way it was at the last commit.
# discard changes to one file
git restore path/to/file.js
# discard changes to everything in the working tree
git restore .
git restore arrived in Git 2.23 (August 2019) to split the overloaded git checkout into two clearer commands: git restore for files, git switch for branches. It is the modern, recommended way to do this, because checkout made it easy to fat-finger a branch name and lose your edits.
On an older Git, the equivalent is:
# pre-2.23 syntax, still works on every version
git checkout -- path/to/file.js
git checkout -- .The -- matters. It tells Git "everything after this is a file path, not a branch or flag," which avoids ambiguity if a file happens to share a name with a branch. Both forms do the same thing: overwrite your working copy with the version from HEAD, the last commit on your current branch.
Discard a change you already staged
If you ran git add on the file, it is staged, and a plain git restore will not touch it. Unstage it first, then discard:
# step 1: move it out of the staging area (the file keeps your edits)
git restore --staged path/to/file.js
# step 2: now discard the edits in the working tree
git restore path/to/file.jsThe two-step is deliberate. git restore --staged only resets the staged copy back to HEAD; your edits are still sitting in the working file. The second git restore is what actually throws those edits away. If you only want to unstage and keep editing, stop after step one (git restore --staged . unstages everything; it replaces the old git reset HEAD file). A common reason to be here at all is that a pull or merge refused to run and complained that your local changes would be overwritten by merge; discarding the staged and working edits is one way to clear that block.
Delete untracked files with git clean
Here is the trap that catches beginners: git restore and git checkout only operate on files Git is already tracking. A brand-new file you have never committed is untracked, and Git will leave it completely alone. To remove untracked files you need git clean.
Always do a dry run first. -n shows what would be deleted without deleting anything:
# DRY RUN: list what clean would remove, delete nothing
git clean -n
git clean -nd # include untracked directories in the previewOnce you are happy with that list, run it for real:
# delete untracked files
git clean -f
# delete untracked files AND untracked directories
git clean -fd
# also remove files your .gitignore is ignoring (build output, etc.)
git clean -fdxThe flags:
-fis "force." Git refuses to delete files without it, on purpose, because this is destructive.-dextends the delete to untracked directories, not just loose files.-xalso removes files matched by your .gitignore rules, likenode_modulesor abuild/folder. Use this one carefully; it wipes things you usually want to keep, like local env files.
A common and damaging mix-up: uppercase -X is not "only untracked files." It removes only ignored files and leaves your real untracked files in place.
| You want to delete | Command |
|---|---|
| Untracked files and folders | git clean -fd |
| Only ignored files (build output, caches) | git clean -fdX |
| Untracked and ignored, everything | git clean -fdx |
git clean -fdx is the nuclear option for a truly pristine checkout. I reach for it when a build is poisoned by stale artifacts and I want the directory to look exactly like a fresh git clone.
Throw everything away and reset to the last commit
To return the entire working tree to the last commit, both tracked edits and untracked junk, you need two commands. reset --hard handles tracked files; clean -fd handles the untracked ones.
# discard all changes to tracked files
git reset --hard
# then remove every untracked file and directory
git clean -fdgit reset --hard rewinds tracked files to HEAD and blows away both staged and unstaged edits in one move. It does not touch untracked files, which is why the clean is still needed for a complete wipe. If you want to reset to a specific commit rather than the current one, name it: git reset --hard <commit-hash>.
A safer middle ground, if you might want the work back later, is to stash instead of discard:
# tuck everything away instead of deleting it
git stash --include-untrackedThat puts your changes on a stack you can restore with git stash pop. When you are not fully sure you want the work gone, stash it; the git stash guide covers the workflow. Stashing costs nothing and buys you an undo.
This is irreversible. Really.
The commands above do not move files to a trash can. They overwrite or delete them at the filesystem level. Git's safety net, git reflog, only tracks committed history. Edits you never committed leave no trace once git restore, git reset --hard, or git clean runs.
- Committed work can almost always be recovered, even after a bad
reset --hard, using the reflog. The commit is still in the object database. - Uncommitted work (anything you discard here) is gone. No reflog entry, no dangling commit, nothing to recover.
So the discipline is simple: run git status to see what you are about to lose, run git clean -n before any real git clean, and when in doubt, git stash instead of discard. Committing early and often also helps, because a commit is a recoverable checkpoint.
How to undo anything in Git
Discarding uncommitted edits is one row of a bigger map. Almost every undo in Git comes down to one question: what state is the change in right now? Pick your row:
| Situation | Command |
|---|---|
| Discard unstaged edits to a file | git restore path/to/file |
| Unstage a staged file (keep the edits) | git restore --staged path/to/file |
| Reword the last commit message | git commit --amend |
| Undo the last commit, keep the changes | git reset --soft HEAD~1 |
| Undo the last commit, throw the changes away | git reset --hard HEAD~1 |
| Undo a commit you have already pushed | git revert <sha> |
| Delete untracked files and folders | git clean -fd (preview with -nd first) |
| Stop tracking a file but keep it on disk | git rm --cached path/to/file |
| Recover something you think you lost | git reflog |
The old muscle memory, git checkout -- file and git reset HEAD file, still works and is not deprecated. But git restore (files) and git switch (branches) were added in Git 2.23 precisely because checkout did too many jobs, and they are what I reach for now. The sections below cover the commit-level rows.
Undo the last commit
To undo your last Git commit but keep all the changes staged, run git reset --soft HEAD~1. The commit disappears, your files are untouched, and everything is staged ready to recommit. Which command you actually want depends on two questions: do you want to keep the changes, and have you already pushed the commit.
# Undo the commit, keep changes staged (most common)
git reset --soft HEAD~1
# Undo the commit, keep changes but unstage them
git reset --mixed HEAD~1 # or just: git reset HEAD~1
# Undo the commit AND throw the changes away (destructive)
git reset --hard HEAD~1
# Just fix the last commit's message or add a forgotten file
git commit --amend
# Undo a commit you already pushed (safe for shared branches)
git revert HEAD
HEAD~1 means "one commit before the current HEAD," so the reset moves the branch pointer back by one commit. After --soft, git status shows your changes staged:
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: src/app.jsRecommit when you are ready with git commit -m "A better message this time". Use --mixed (the default mode) instead when you want to re-stage selectively, for example to split one fat commit into several. Use --hard only for a commit full of junk you never want to see again; if you panic afterwards, the reflog usually still has it.
Fix the last commit with amend
Sometimes you do not want to undo the commit at all, just fix a typo in the message or add a forgotten file:
git commit --amend -m "The corrected commit message"
# add a file you forgot, keeping the same message
git add forgotten-file.js
git commit --amend --no-edit--amend replaces the last commit with a new one (new hash), so it is still a history rewrite.
Have you pushed? That decides everything
- Not pushed yet (local only): rewrite history freely.
resetandamendare fine. - Already pushed to a shared branch: do not rewrite history; other people may have pulled that commit. Use
revert(next section). Rewriting a commit someone already pulled is how you end up with divergent branches and force-push arguments.
| Situation | Command | Keeps changes? | Rewrites history? |
|---|---|---|---|
| Committed too early, want to recommit | git reset --soft HEAD~1 | Yes, staged | Yes (local only) |
| Want to re-stage selectively | git reset HEAD~1 | Yes, unstaged | Yes (local only) |
| Commit was pure junk, drop it | git reset --hard HEAD~1 | No, discarded | Yes (local only) |
| Just a bad message or a missing file | git commit --amend | Yes | Yes (local only) |
| Commit is already pushed and shared | git revert HEAD | New inverse commit | No, adds a commit |
HEAD~2 walks back two commits, HEAD~3 three, and so on: git reset --soft HEAD~3 undoes the last three local commits but keeps their combined changes staged. For surgical edits across several commits (reword, drop, combine specific ones), interactive rebase is the right tool.
git reset vs git revert
git reset and git revert both undo work in opposite ways. reset moves your branch pointer backward and rewrites history, as if the later commits never happened. revert leaves history intact and adds a brand new commit that cancels out an earlier one. The rule: reset on commits you have not shared yet, revert on anything already pushed to a branch other people pull from.
git reset | git revert | |
|---|---|---|
| What it does | Moves the branch pointer to an older commit | Adds a new commit that reverses an old one |
| History | Rewritten (commits dropped from the branch) | Preserved (everything stays in the log) |
| Safe on shared branches | No | Yes |
| Creates a new commit | No | Yes (one per reverted commit) |
| Touches working tree | Depends on mode (--soft, --mixed, --hard) | Yes, applies the inverse diff |
| Recoverable if wrong | Via the reflog, time-limited | Trivially, just revert the revert |
The three reset modes
git reset always moves the branch pointer. What it does to the staging area and your working files depends on the mode:
| Mode | Branch pointer | Staging area | Working tree |
|---|---|---|---|
--soft | Moved back | Kept (changes stay staged) | Kept |
--mixed (default) | Moved back | Reset (changes unstaged) | Kept |
--hard | Moved back | Reset | Reset (changes discarded) |
Why revert is the safe choice on shared branches
Once you have pushed a commit, its hash is part of the history everyone else has pulled. If you reset past it and force-push, your branch no longer contains commits other people's branches still do, and the next git pull turns into diverged history, conflicts, or someone re-pushing the commits you tried to delete. git revert only ever adds a commit, so teammates pull a normal change and nothing needs reconciling. On a protected main branch, the force-push a reset would need is blocked outright anyway.
# Undo a specific public commit by hash (find it with git log --oneline)
git revert 9f1c0a2
git push
# Undo a range (each commit gets its own revert, newest first)
git revert HEAD~3..HEAD
# Stage the inverse changes without committing yet, so you can group them
git revert --no-commit 9f1c0a2Git opens an editor with a prefilled Revert "the original subject" message; save it, or pass --no-edit. Reverting a merge commit needs the -m parent flag; the git revert docs cover the mainline-parent rule. If a revert turns out to be wrong, revert the revert.
One caveat on secrets: revert undoes the change, but a committed API key still sits in the history and can be checked out from the old commit. Rotate the key, and if it must be gone from history, follow removing a secret from Git history.
Recover lost commits with git reflog
If a bad git reset, a botched rebase, or a deleted branch seems to have wiped out commits, the work is almost always still there. Git keeps a log of every place HEAD has pointed, the reflog. Run git reflog, find the line just before things went wrong, and point a reference back at that hash.
git reflog
e3a1f0c (HEAD -> main) HEAD@{0}: reset: moving to HEAD~3
9b7d2a4 HEAD@{1}: commit: add password reset endpoint
1c8e5f9 HEAD@{2}: commit: wire up email templates
4f6b0d3 HEAD@{3}: commit: scaffold auth routes
a2c9e88 HEAD@{4}: checkout: moving from develop to mainEach line is one move of HEAD: the hash, HEAD@{n} (how many moves ago, 0 being the latest), and the action. The reflog is local to your clone (it lives in .git/logs/ and is never pushed), and it records the old hashes that a rebase or reset --hard hides from git log.
A commit is an immutable object; a branch is a movable label. "Losing" a commit usually means the label moved off it. Git only prunes unreachable objects during garbage collection, and by default reflog entries for commits no longer reachable from any branch are kept for 30 days (gc.reflogExpireUnreachable), versus 90 days (gc.reflogExpire) for commits still on a branch. The commits you recover are the unreachable ones, so 30 days is the window that matters: weeks, not seconds.
After a bad reset
You ran git reset --hard HEAD~3 and three commits vanished. In the reflog above, HEAD@{1} (9b7d2a4) is where you were right before the reset:
git reset --hard 9b7d2a4git reset --hard HEAD@{1} also works, but the explicit hash is harder to get wrong. Stash any uncommitted edits first, since reset --hard discards them.
After a botched rebase
A rebase leaves a clear marker in the reflog:
7d4c1a2 (HEAD -> feature) HEAD@{0}: rebase (finish): returning to refs/heads/feature
7d4c1a2 HEAD@{1}: rebase (squash): combine auth commits
b8e3f01 HEAD@{2}: rebase (start): checkout main
2a9c6d4 HEAD@{3}: commit: add password reset endpointThe entry just before rebase (start), HEAD@{3} here, is your branch exactly as it stood before the rebase. git reset --hard HEAD@{3} throws the rebase away. The HEAD@{n} numbers shift every time HEAD moves, so always read your own reflog.
A deleted branch
git branch -D feature/checkoutDeleted branch feature/checkout (was 5f2a8c1).Git prints the hash right there. If you missed it, find the branch's last position in git reflog, then recreate it: git branch feature/checkout 5f2a8c1.
reset, checkout, branch, or cherry-pick?
| Command | What it does | Use it when |
|---|---|---|
git reset --hard <hash> | Moves your current branch tip to that commit and matches the working tree to it | You want the current branch to become the recovered state and have no uncommitted work to lose |
git checkout <hash> | Puts you on the commit in detached HEAD, leaving every branch untouched | You want to inspect the commit first |
git branch <name> <hash> | Creates a new branch pointing at the commit | You are recovering a deleted branch, or want the old state on its own branch |
git cherry-pick <hash> | Replays just that commit on top of your current branch | You only need one lost commit's changes back |
git switch -c recovered-work <hash> creates the branch and moves onto it in one step. When unsure, git checkout <hash> is the gentlest move.
The reflog cannot help with uncommitted work (it tracks commits, not your working tree), with a commit that only lived in a clone you deleted and was never pushed, or after garbage collection past the grace period (git gc --prune=now prunes unreachable commits immediately). If you ever pushed the work, git fetch and the remote-tracking branches still have it.
Remove a file from Git without deleting it
To stop Git tracking a file but keep it on your disk, run git rm --cached <file>. The --cached flag removes the file from the index without touching the copy in your working directory. This is the fix for a .env, a node_modules folder, or an IDE config you committed by accident.
git rm --cached config.local.jsonrm 'config.local.json'The "rm" looks alarming, but the file is still in your folder. git status shows it twice, as a staged deletion and as an untracked file:
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: config.local.json
Untracked files:
(use "git add <file>..." to include in what will be committed)
config.local.jsonCommit the removal, then add the path to .gitignore so the next git add . does not stage it straight back in:
git commit -m "Stop tracking config.local.json"
echo "config.local.json" >> .gitignore
git add .gitignore
git commit -m "Ignore config.local.json"Adding a tracked file to .gitignore does nothing on its own; .gitignore only affects untracked files, which is why git rm --cached comes first. For a whole folder, add -r:
git rm -r --cached node_modules
echo "node_modules/" >> .gitignoreIt prints one rm line per file, which can be thousands for node_modules; the folder stays on disk.
| Command | Removes from Git index | Deletes file on disk | When to use it |
|---|---|---|---|
git rm --cached file | Yes | No | Stop tracking, keep the file locally |
git rm file | Yes | Yes | Delete the file from the repo and your disk |
rm file (plain shell) | No (until you commit) | Yes | Delete on disk; Git records a deletion you still commit |
If you ran git rm without --cached and the file vanished, bring it back before committing with git restore --staged config.local.json then git restore config.local.json (on Git before 2.23, git checkout HEAD -- config.local.json). If you already committed the deletion, git checkout HEAD~1 -- config.local.json restores it from the previous commit.
The file is still in your history. git rm --cached stops tracking from this commit forward; it does not scrub earlier commits. If the file held a secret, treat it as compromised: rotate it, then rewrite history to purge it.
FAQ
Sources
Authoritative references this article was fact-checked against.
- git-restore: official Git documentationgit-scm.com
- git-clean: official Git documentationgit-scm.com
- Pro Git: Git Basics, Undoing Thingsgit-scm.com
- git-reset: official Git documentationgit-scm.com
- git-revert: official Git documentationgit-scm.com
- git-commit: official Git documentationgit-scm.com
- git-reflog: Git official documentationgit-scm.com
- Pro Git: Reset Demystifiedgit-scm.com
- Pro Git: Maintenance and Data Recoverygit-scm.com
- git-rm: official Git documentationgit-scm.com





