How to fix error: The following untracked working tree files would be removed by reset in Git
This Git error occurs when you run `git reset` and there are untracked files in your working directory that would be overwritten or removed by files from the target commit. Git prevents the reset to protect your untracked work from being silently deleted.
Quick fix
Fixes error: The following untracked working tree files would be removed by reset
# List all untracked files
git status --porcelain | grep '^??'
# Or see a clean list
git ls-files --others --exclude-standard- Review the files that are blocking the reset
- Option 1: Stash untracked files before resetting
- Option 2: Move untracked files to a safe location
- Option 3: Remove untracked files with git clean
# List all untracked filesgit status --porcelain | grep '^??'# Or see a clean listgit ls-files --others --exclude-standarderror: The following untracked working tree files would be removed by reset
What this error means
This error appears when you attempt to run `git reset` (typically `git reset --hard`) to move your branch to a different commit, but your working directory contains untracked files that would collide with files from the target commit. Git refuses to proceed because continuing would permanently delete your untracked files. Unlike tracked files (which Git can restore from commit history), untracked files have never been committed and exist only in your working directory. If Git were to overwrite them during the reset, there would be no way to recover them from Git's history. The error message specifically lists which untracked files are blocking the reset operation. These are files that: 1. Exist in the commit you're resetting to, AND 2. Currently exist in your working directory as untracked files Git is giving you a chance to either save these files, move them elsewhere, or explicitly tell Git it's okay to remove them.
Typical symptoms
- git reset --hard fails with a list of untracked files
- git reset to a specific commit or branch is blocked
- Cannot reset after a failed merge or rebase
- Reset fails even though you want to discard all changes
- Error appears when trying to sync with a remote branch
- git pull with rebase fails due to untracked file conflicts
- Automation scripts or CI pipelines fail during git reset operations
Common causes
- Untracked files in your working directory match files that exist in the target commit
- Build artifacts or generated files were created that match tracked files in another branch
- Node_modules, vendor, or dependency folders were not gitignored but exist in the target commit
- IDE or editor configuration files conflict between branches
- Files were added in the target branch that you also created locally but never committed
- Someone else added files to the repository that you independently created locally
- Log files or cache files were created that match paths in the target commit
How to fix "error: The following untracked working tree files would be removed by reset"
First, examine the untracked files that Git is warning you about. The error message lists them, but you can also review your untracked files:
# List all untracked files
git status --porcelain | grep '^??'
# Or see a clean list
git ls-files --others --exclude-standardDecide whether these files contain important work you need to keep, or if they can be safely removed.
If the untracked files contain work you want to preserve, stash them before resetting:
# Stash including untracked files
git stash push --include-untracked -m "Stashing before reset"
# Now perform the reset
git reset --hard <target-commit>
# Later, you can view your stash
git stash list
# And restore if needed (may cause conflicts)
git stash popNote: When you pop the stash, the previously untracked files will be restored. If they conflict with files now in your working directory, you'll need to resolve those conflicts manually.
If you want to keep the files but outside Git's control, move them temporarily:
# Create a backup directory outside your repo
mkdir -p ~/backup-untracked-files
# Move the specific files (use the paths from the error message)
mv path/to/untracked-file.txt ~/backup-untracked-files/
# Now reset will succeed
git reset --hard <target-commit>
# Move files back if needed
mv ~/backup-untracked-files/untracked-file.txt ./path/to/This approach is simple and gives you full control over which files to preserve.
If you don't need the untracked files and want to remove them, use git clean:
# FIRST: Preview what would be deleted (dry run)
git clean -n -d
# Remove untracked files (requires -f for force)
git clean -f
# Remove untracked files AND directories
git clean -f -d
# Now reset will succeed
git reset --hard <target-commit>Important flags:
- -n or --dry-run: Show what would be deleted without deleting
- -f or --force: Required to actually delete files
- -d: Also remove untracked directories
- -x: Also remove ignored files (like node_modules, build folders)
- -X: Remove ONLY ignored files
Warning: git clean -f permanently deletes files. Always run with -n first!
If you only want to remove the specific files mentioned in the error:
# Remove specific files (use paths from error message)
rm path/to/conflicting-file.txt
rm -r path/to/conflicting-directory/
# Or use git clean with a path
git clean -f path/to/conflicting-file.txt
# Now reset will work
git reset --hard <target-commit>This is the most surgical approach - you remove only what's blocking the reset.
If the conflicting files are generated artifacts you never want tracked, add them to .gitignore before cleaning:
# Add patterns to .gitignore
echo "build/" >> .gitignore
echo "*.log" >> .gitignore
echo ".env.local" >> .gitignore
# Stage the .gitignore change
git add .gitignore
# Now clean with -X to remove only ignored files
git clean -f -X -d
# Proceed with reset
git reset --hard <target-commit>This ensures similar files won't cause the same problem in the future.
For a complete reset that removes all local changes AND untracked files:
# Preview everything that will be affected
git status
git clean -n -d
# Perform the full reset
git reset --hard <target-commit>
git clean -f -d
# Verify the result
git statusOne-liner for experienced users:
git reset --hard <target-commit> && git clean -fdThis is commonly used to get a pristine working directory matching a specific commit.
Advanced notes
### Understanding Why This Happens
The distinction between tracked and untracked files is crucial:
| File Type | Definition | Can Git Restore? |
|-----------|------------|------------------|
| Tracked | Previously committed or staged | Yes, from history |
| Untracked | Never added to Git | No |
| Ignored | Matches .gitignore pattern | No |
When git reset --hard moves HEAD to a different commit, it updates your working directory to match that commit. If a file exists in the target commit but is currently untracked in your working directory, Git would have to:
1. Delete your untracked file
2. Replace it with the version from the commit
Git refuses to do this silently because step 1 is irreversible for untracked files.
### Common Scenarios
After a failed merge or rebase:
# The merge created conflicts, you want to abort
git merge --abort # This might fail too
# Use clean + reset
git clean -fd
git reset --hard HEADResetting to match remote exactly:
# Fetch latest remote state
git fetch origin
# Reset to match remote branch exactly
git reset --hard origin/main
git clean -fdIn CI/CD pipelines:
# Start with guaranteed clean state
git clean -ffdx
git reset --hard HEAD
git checkout <branch>Note the double -f (-ff) - this is needed to remove nested git repositories.
### The -x vs -X Difference
- git clean -x: Removes ALL untracked files, including those matched by .gitignore
- git clean -X: Removes ONLY files matched by .gitignore (ignored files)
Use -X when you want to clean build artifacts but keep truly new files.
### Interactive Cleaning
For careful file removal, use interactive mode:
git clean -i -dThis presents a menu where you can:
- Filter by pattern
- Select specific files/directories
- Preview before deletion
- Clean selected items
### Protecting Important Untracked Files
If you frequently have untracked files you want to protect:
1. Add to .gitignore if they should never be tracked
2. Use git stash -u habitually before risky operations
3. Consider using git worktrees for parallel work:
git worktree add ../feature-work feature-branch### Reset Modes Reference
| Mode | HEAD | Index | Working Dir | Untracked |
|------|------|-------|-------------|-----------|
| --soft | Yes | No | No | No |
| --mixed (default) | Yes | Yes | No | No |
| --hard | Yes | Yes | Yes | Conflicts only |
| --hard + clean | Yes | Yes | Yes | Yes |
Only --hard mode can trigger the "untracked files would be removed" error because it's the only mode that modifies the working directory.
### Preventing Future Conflicts
1. Maintain comprehensive .gitignore files for your project type
2. Run `git status` before reset operations to see untracked files
3. Use `git stash -u` as a habit before potentially destructive operations
4. In CI, always start with `git clean -ffdx` for reproducible builds
Frequently asked questions
- What does "error: The following untracked working tree files would be removed by" mean?
- This error appears when you attempt to run git reset (typically git reset --hard) to move your branch to a different commit, but your working directory contains untracked files that would collide with files from the target commit. Git refuses to proceed because continuing would permanently delete your untracked files. Unlike tracked files (which Git can restore from commit history), untracked files have never been committed and exist only in your working directory.
- How do I fix "error: The following untracked working tree files would be removed by"?
- This Git error occurs when you run git reset and there are untracked files in your working directory that would be overwritten or removed by files from the target commit. Git prevents the reset to protect your untracked work from being silently deleted.
- What causes "error: The following untracked working tree files would be removed by"?
- Untracked files in your working directory match files that exist in the target commit Build artifacts or generated files were created that match tracked files in another branch Node_modules, vendor, or dependency folders were not gitignored but exist in the target commit
- Is the "error: The following untracked working tree files would be removed by" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Git Official Documentation
- ๐Git Docs - git-reset(Official Docs)
- ๐Git Docs - git-clean(Official Docs)
- ๐ฌStack Overflow - git reset --hard leaves untracked files(Stack Overflow)
- ๐Atlassian - Remove Untracked Files in Git(Official Docs)
- ๐Git Tower - Remove Untracked Files with Git Clean(Official Docs)
- ๐Git Docs - git-stash(Official Docs)