Git
Version Control
Coding
Programming
Software Development

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.

Browse interview questions

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:

bash
1git status --short
2git diff --name-status
3git diff --cached --name-status
4git clean -nd

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.

bash
git stash push -u -m "safety backup before full cleanup"
git stash list

If the changes are substantial or worth sharing, a temporary branch and commit can be safer:

bash
git switch -c backup/full-cleanup-2026-03-07
git add -A
git commit -m "Temporary backup before cleanup"

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.

bash
git restore --staged -- .
git restore --worktree -- .

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.

bash
git clean -fd

If you also want ignored files gone, use:

bash
git clean -fdx

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:

bash
git reset --hard HEAD
git clean -fd

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.

bash
git status --short
git clean -nd

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:

bash
git stash show --name-only stash@{0}
git stash apply stash@{0}

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, and git clean -nd.
  • Create a stash or backup branch if recovery might matter.
  • Use restore for tracked files and git clean for new files.
  • Treat git clean -fdx as a high-risk cleanup reserved for intentional full resets.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.