Class 10 of 50 • Everyday Workflow

Git Undoing Changes: Mastering git restore, git clean & Discarding Edits

Every developer makes mistakes—editing the wrong function, introducing unintended syntax errors, or staging files prematurely. Git provides powerful, surgical mechanisms to undo mistakes at every level of the three-tree lifecycle. In Git 2.23+, the dedicated git restore command makes this process clear and safe.

1. Discarding Unstaged Changes in the Working Directory

If you modified a file in your editor but haven't run git add yet, and you want to throw away all your edits and return the file to the exact state of the latest commit:

# Discard changes to a single file:
$ git restore src/app.js

# Discard changes to multiple specific files:
$ git restore styles.css index.html

# Discard all unstaged changes across your entire project:
$ git restore .

Caution: Discarded Working Changes Cannot Be Recovered!

Unlike committed snapshots, uncommitted edits in your working directory exist only in your operating system's filesystem RAM/disk. Once you run git restore . on unstaged files, those edits are permanently erased!

2. Unstaging Files from the Staging Area (git restore --staged)

If you ran git add on a file by mistake, but you still want to keep your edits in your working directory, simply unstage the file:

# Unstage a single file (moves it from Index back to Working Directory):
$ git restore --staged package.json

# Unstage all staged files:
$ git restore --staged .

After running this, package.json remains modified on your disk, but it is no longer marked for the next commit snapshot.

3. Removing Untracked Files with git clean

git restore only operates on files that Git already tracks. If you created 10 new experimental files, generated log files, or downloaded build artifacts that are untracked, use git clean to purge them:

# Step 1: ALWAYS run a dry run first to see what WILL be deleted:
$ git clean -dn
Would remove test_draft.js
Would remove temp_output.json

# Step 2: Delete untracked files permanently:
$ git clean -df
# -d = include untracked directories
# -f = force deletion

Summary: The Modern Undoing Matrix

What do you want to undo? Where are the changes? Command to Run
Discard file edits and reset to HEAD Working Directory (Unstaged) git restore filename
Unstage a file, keeping edits on disk Staging Area (Index) git restore --staged filename
Delete all newly created untracked files Working Directory (Untracked) git clean -df
Revert an entire commit recorded in history Local Repository (Committed) git revert [commit-hash]

Why did Git introduce git restore?

Historically, Git overloaded the git checkout command to perform two completely different tasks: switching branches (git checkout branch-name) and discarding file changes (git checkout -- filename). Beginners frequently confused the two and accidentally wiped out their uncommitted work. Starting in Git version 2.23 (released August 2019), Git split these responsibilities into two dedicated, unambiguous commands: git switch (for branches) and git restore (for files).

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

git reset vs git revert Deep Dive

Understand when to rewrite local history vs when to emit safe public reverts.

Amending Latest Commits

Quickly patch accidental commits without creating messy follow-up fix commits.

Stashing Work Temporarily

Shelve half-written code instead of discarding changes with git restore.

Recovering Lost Work with Reflog

Restore commits and branches discarded by mistaken resets or clean operations.

Frequently Asked Questions (FAQ) - Student & Interview Review

In older Git versions (prior to 2.23), developers used `git checkout -- [filename]` to discard working tree modifications, and `git reset HEAD [filename]` to unstage files. While both legacy commands still work for backward compatibility, `git restore` is the modern standard.
By default, `git clean -df` ignores files listed in `.gitignore` to prevent wiping out dependencies or `.env`. If you intentionally want to delete ignored files as well (e.g. for a total clean rebuild), pass the `-x` flag: `git clean -dfx`.
If you physically deleted a tracked file in your working folder, run `git restore [filename]`. Git will immediately restore the file from HEAD to your working directory.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to git reset vs git revert → Previous: ← .gitignore Mastery | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!