How can I revert all uncommitted changes in Git, including files and folders, and remove newly created files from both the working tree and index?
Git provides several effective methods to revert all uncommitted changes and remove newly created files from both the working tree and index. The most comprehensive approach combines git reset to remove changes from the index and git clean to delete untracked files, ensuring your working directory returns to a clean state identical to your last commit.
Contents
- Understanding Git’s Areas
- Method 1: Using
git reset --hardandgit clean - Method 2: Using
git restore - Method 3: Using
git checkout - Method 4: Interactive Reset with
git reset --hard HEAD - Safety Precautions and Best Practices
- Recovering Accidentally Removed Files
- Summary of Commands
Understanding Git’s Areas
Before reverting changes, it’s essential to understand Git’s three main areas:
- Working Directory: Your actual files on disk where you make edits
- Index (Staging Area): Files that are staged and ready for the next commit
- Repository: Your committed changes stored in Git
When you want to revert all uncommitted changes, you need to address both the working directory and the index, plus remove any newly created files that Git isn’t tracking.
Method 1: Using git reset --hard and git clean
This is the most comprehensive and commonly recommended approach for completely reverting all uncommitted changes.
# Step 1: Reset the index to match the last commit
git reset --hard HEAD
# Step 2: Remove all untracked files and directories
git clean -fd
Breaking this down:
-
git reset --hard HEAD: This command:- Resets the index to match
HEAD(your last commit) - Resets the working directory to match
HEAD - Discards all changes both staged and unstaged
- Resets the index to match
-
git clean -fd:-f: Forces the removal of files-d: Removes directories in addition to files- Removes all files that aren’t tracked by Git
Alternative with safety prompts:
If you prefer to be prompted before each file deletion:
git clean -fdi
The -i flag provides an interactive mode where you can selectively choose which files to remove.
Method 2: Using git restore
For Git versions 2.23 and later, git restore provides a more intuitive approach:
# Restore all tracked files to their committed state
git restore .
# Remove all untracked files
git clean -fd
The git restore . command:
- Restores all tracked files in the working directory to their committed state
- Only affects files that Git is already tracking
- Doesn’t remove newly created files (that’s why
git cleanis still needed)
More specific restore options:
# Restore specific file
git restore filename.txt
# Restore from a specific commit
git restore --source=HEAD~1 filename.txt
Method 3: Using git checkout
Older versions of Git used git checkout for this purpose. While still functional, git restore is now preferred in newer Git versions:
# For older Git versions (pre-2.23)
git checkout -- .
# Then clean untracked files
git clean -fd
The git checkout -- . command restores all files in the working directory to match the index, and since we haven’t staged any changes, it effectively reverts to the last commit state.
Method 4: Interactive Reset with git reset --hard HEAD
Sometimes you want to be more selective about what you’re removing. Here’s an interactive approach:
# First, see what changes will be discarded
git status
# Reset but keep some changes if needed
git reset --hard HEAD
# Check what's still untracked
git clean -n
# Actually remove the untracked files
git clean -fd
The git clean -n (dry run) shows you what would be deleted without actually deleting anything, giving you a chance to review first.
Safety Precautions and Best Practices
Before running these destructive commands, consider these safety measures:
Always check what you’re about to remove:
# See what changes will be discarded
git status
# See what untracked files will be deleted
git clean -n
# Take a backup if you're unsure
git stash push -m "backup before reset"
Safety-first approach:
# Interactive clean - safe but requires confirmation for each file
git clean -fdi
# Or use dry-run first
echo "About to run:"
echo " git reset --hard HEAD"
echo " git clean -fd"
read -p "Press Enter to continue or Ctrl+C to abort..."
Important notes:
- These operations are irreversible (except through
git reflog) - Always commit important changes before resetting
- Use
git stashto temporarily save changes you might need later
Recovering Accidentally Removed Files
If you accidentally removed files you needed, you might be able to recover them:
Using git reflog:
# Check your recent actions
git reflog
# Find the commit before the reset
# Then restore from that commit
git restore --source=HEAD@{2} filename.txt
Using git fsck:
# Find dangling commits/objects
git fsck --lost-found
# This might help recover lost commits
Summary of Commands
Here’s a quick reference table for the different approaches:
| Method | Command | Pros | Cons | Best For |
|---|---|---|---|---|
| Reset + Clean | git reset --hard HEAD + git clean -fd |
Complete cleanup, removes everything | Irreversible, no safety net | Total reset to clean state |
| Restore + Clean | git restore . + git clean -fd |
Modern syntax, clearer intent | Requires Git 2.23+ | Modern Git workflows |
| Checkout + Clean | git checkout -- . + git clean -fd |
Works on older Git versions | Less intuitive syntax | Legacy Git environments |
| Interactive | git reset --hard HEAD + git clean -fdi |
Safe, allows selective removal | Requires manual confirmation | When you want to review |
Sources
- Official Git Documentation - git-reset
- Official Git Documentation - git-restore
- Official Git Documentation - git-clean
- Pro Git Book - Reset Demystified
- Atlassian Git Tutorial - Undoing Changes
Conclusion
Reverting all uncommitted changes in Git requires addressing both the working directory and index, plus removing untracked files. The most reliable approach combines git reset --hard HEAD to revert tracked changes and git clean -fd to remove untracked files. Always use dry-run options like git clean -n before executing destructive commands, and consider using git stash to temporarily save changes you might need later. For modern Git versions (2.23+), git restore provides a more intuitive alternative to git checkout for restoring files to their committed state. Remember that these operations are irreversible, so always commit important changes before performing resets or use safety mechanisms like interactive modes to review what will be deleted.