How to fix error: The following untracked working tree files would be removed by reset in Git

GitBEGINNERMEDIUMUpdated Dec 6, 2025ยท 231 views

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
  1. Review the files that are blocking the reset
  2. Option 1: Stash untracked files before resetting
  3. Option 2: Move untracked files to a safe location
  4. Option 3: Remove untracked files with git clean
git โ€” error
# List all untracked files
git status --porcelain | grep '^??'
# Or see a clean list
git ls-files --others --exclude-standard
error: 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

  1. Untracked files in your working directory match files that exist in the target commit
  2. Build artifacts or generated files were created that match tracked files in another branch
  3. Node_modules, vendor, or dependency folders were not gitignored but exist in the target commit
  4. IDE or editor configuration files conflict between branches
  5. Files were added in the target branch that you also created locally but never committed
  6. Someone else added files to the repository that you independently created locally
  7. 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"

1Review the files that are blocking the 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:

bash
# List all untracked files
git status --porcelain | grep '^??'

# Or see a clean list
git ls-files --others --exclude-standard

Decide whether these files contain important work you need to keep, or if they can be safely removed.

2Option 1: Stash untracked files before resetting

If the untracked files contain work you want to preserve, stash them before resetting:

bash
# 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 pop

Note: 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.

3Option 2: Move untracked files to a safe location

If you want to keep the files but outside Git's control, move them temporarily:

bash
# 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.

4Option 3: Remove untracked files with git clean

If you don't need the untracked files and want to remove them, use git clean:

bash
# 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!

5Option 4: Clean only the conflicting files

If you only want to remove the specific files mentioned in the error:

bash
# 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.

6Option 5: Add files to .gitignore before cleaning

If the conflicting files are generated artifacts you never want tracked, add them to .gitignore before cleaning:

bash
# 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.

7Combine clean and reset in one operation

For a complete reset that removes all local changes AND untracked files:

bash
# 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 status

One-liner for experienced users:

bash
git reset --hard <target-commit> && git clean -fd

This 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:

bash
# The merge created conflicts, you want to abort
git merge --abort  # This might fail too

# Use clean + reset
git clean -fd
git reset --hard HEAD

Resetting to match remote exactly:

bash
# Fetch latest remote state
git fetch origin

# Reset to match remote branch exactly
git reset --hard origin/main
git clean -fd

In CI/CD pipelines:

bash
# 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:

bash
git clean -i -d

This 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:

bash
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