git, undo all working dir changes including new files
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Undoing all working-directory changes in Git, including new files, is one of the fastest ways to recover from a failed experiment and one of the fastest ways to destroy work you did not mean to lose. The trick is to treat the cleanup as a controlled destructive operation. Inspect first, make a safety copy if needed, then remove tracked and untracked changes deliberately instead of running commands from memory.
Know What Git Means by "All Changes"
Git divides local state into categories, and different commands affect different categories:
- tracked unstaged changes
- tracked staged changes
- untracked files and directories
- ignored files such as build output
That means there is no single safe command named "undo everything." You usually need a small sequence, and the correct sequence depends on whether ignored files should survive.
Start with inspection:
The dry-run git clean -nd preview is especially important because it shows which untracked files would be deleted.
Make a Backup Before Destructive Cleanup
If there is even a small chance the work will be needed later, create a safety snapshot first. The quickest option is usually a stash that includes untracked files.
If the changes are substantial or worth sharing, a temporary branch and commit can be safer:
That is heavier than a stash, but it is easier to inspect and recover from later.
Restore Tracked Files Explicitly
In modern Git, tracked-file cleanup is clearer with restore.
The first command removes tracked changes from the index. The second restores tracked files in the working tree from HEAD. At this point, tracked files are back to the committed state, but untracked files still remain.
That separation is useful because it prevents the mistaken assumption that restore also deletes new files.
Remove New Files With git clean
Untracked files and directories are handled by git clean.
If you also want ignored files gone, use:
The -x flag is much more destructive because it removes ignored content such as build artifacts, caches, or environment files. Use it only when you really want a fully pristine checkout.
Older Git Workflows Still Use reset --hard
In older documentation, you will often see:
This still works. It is concise and widely known. The downside is that it hides several actions inside one command and makes it easier to use destructively without thinking through staged versus unstaged state. The newer restore form is usually easier to explain and review.
Verify the Cleanup Actually Finished
After the cleanup, verify again rather than assuming success.
An empty git status --short and no dry-run clean output usually means the working directory is clean. If ignored files were intentionally preserved, that is the moment to confirm they are the only remaining local artifacts.
Recovery Depends on the Backup You Made
If you cleaned too much, recovery is only as good as the safety step you took earlier. A stash can be inspected or reapplied:
A backup branch can be revisited or cherry-picked selectively. Without a stash, branch, editor local history, or filesystem snapshot, local uncommitted work may be gone permanently.
That is why the preview and backup steps are not paranoia. They are the real protection in this workflow.
Common Pitfalls
The biggest mistake is assuming git restore deletes untracked files. Another is running git clean -fd or -fdx without a dry run and deleting important local-only files. Teams also forget staged changes, clean from the wrong repository directory, or skip the backup because the work "probably is not needed," then discover later that it was.
Summary
- Separate tracked, staged, untracked, and ignored cleanup in your mind before you run commands.
- Inspect first with
status,diff, andgit clean -nd. - Create a stash or backup branch if recovery might matter.
- Use
restorefor tracked files andgit cleanfor new files. - Treat
git clean -fdxas a high-risk cleanup reserved for intentional full resets.
Related reading
- git undo all working dir changes including new files
- Git undo changes in some files
- Git undo local branch delete
- git update-index --assume-unchanged and git reset
- Git update submodules recursively
- Git vs Mercurial vs SVN
- Git What's the best practice to git clone into an existing folder?
- git, whitespace errors, squelching and autocrlf, the definitive answers
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.