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

GitBEGINNERLOWUpdated May 28, 2026ยท 370 views

Fix Git's "untracked working tree files would be overwritten by checkout" error. Move, remove, or commit the conflicting files, then retry the checkout or rebase.

Quick fix

Fixes error: The following untracked working tree files would be overwritten by checkout

# The error message shows which files are affected, for example:
# error: The following untracked working tree files would be overwritten by checkout:
#     src/config.js
#     .env.local
  1. Review the conflicting files
  2. Option A: Stash untracked files (keep them temporarily)
  3. Option B: Remove untracked files (if not needed)
  4. Option C: Add and commit the files (if they should be tracked)
git โ€” error
# The error message shows which files are affected, for example:
# error: The following untracked working tree files would be overwritten by checkout:
# src/config.js
# .env.local
error: The following untracked working tree files would be overwritten by checkout

What this error means

When you run `git checkout`, `git switch`, or `git rebase`, Git needs to update your working directory to match the target commit or branch. If the target contains files that also exist in your working directory as untracked files (files not yet added to Git), Git will refuse to proceed. This is a safety mechanism. Git cannot restore untracked files because they have never been committed. If Git were to overwrite them with the version from the target branch, your local changes would be permanently lost. The error typically lists the specific files that would be affected, allowing you to decide how to handle them before proceeding.

Typical symptoms

  • Git checkout command fails with 'untracked working tree files' error
  • Git rebase aborts before starting with file conflict warning
  • Git switch command refuses to change branches
  • Error message lists specific file paths that would be overwritten
  • The operation works after removing or stashing the listed files

Common causes

  1. You created new files locally that also exist in the target branch
  2. Build artifacts or generated files match files tracked in another branch
  3. A colleague added files to a branch that you also created locally
  4. Files were previously tracked, removed from Git, but still exist locally
  5. Case sensitivity differences between your filesystem and Git's records
  6. IDE or editor generated configuration files that are tracked in another branch

How to fix "error: The following untracked working tree files would be overwritten by checkout"

1Review the conflicting files

First, examine the files Git lists in the error message. Determine if they contain important work you want to keep:

bash
# The error message shows which files are affected, for example:
# error: The following untracked working tree files would be overwritten by checkout:
#     src/config.js
#     .env.local

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

2Option A: Stash untracked files (keep them temporarily)

If you want to preserve the untracked files and restore them later, use git stash with the -u flag to include untracked files:

bash
# Stash all changes including untracked files
git stash -u

# Now perform your checkout/rebase
git checkout target-branch
# or
git rebase main

# Later, restore your stashed files
git stash pop

Note: git stash pop may cause merge conflicts if the restored files conflict with the branch content.

3Option B: Remove untracked files (if not needed)

If the files are not important (e.g., build artifacts, temporary files), you can remove them with git clean:

bash
# First, do a dry run to see what would be deleted
git clean -n -d

# If the list looks correct, remove the files
git clean -f -d

# Now proceed with your operation
git checkout target-branch

Warning: git clean permanently deletes files. Always run with -n (dry run) first to verify.

4Option C: Add and commit the files (if they should be tracked)

If these files represent work that should be part of your repository, commit them first:

bash
# Add the untracked files
git add src/config.js .env.local

# Commit them
git commit -m "Add configuration files"

# Now checkout/rebase will handle them as tracked files
git checkout target-branch

This converts the conflict from an untracked file issue to a normal merge conflict, which Git can handle.

5Option D: Move files temporarily (manual backup)

For maximum safety, manually move the files out of the repository:

bash
# Move files to a safe location
mv src/config.js ~/backup/
mv .env.local ~/backup/

# Perform your Git operation
git checkout target-branch

# Move files back if needed (they'll be untracked again)
mv ~/backup/config.js src/
mv ~/backup/.env.local .

This approach gives you complete control and doesn't risk losing any data.

6Option E: Force checkout (use with caution)

If you're certain you don't need the untracked files, you can force the checkout:

bash
# Force checkout - WILL DELETE untracked files that conflict
git checkout -f target-branch

# Or with git switch (Git 2.23+)
git switch -f target-branch

Warning: This permanently deletes the conflicting untracked files. Only use this if you're certain the files are not needed.

Advanced notes

Preventing future occurrences:

Use a .gitignore file to prevent build artifacts, IDE files, and environment-specific files from becoming a source of conflicts:

gitignore
# Build outputs
dist/
build/
node_modules/

# IDE files
.idea/
.vscode/
*.swp

# Environment files
.env.local
.env.*.local

Case sensitivity issues:

On case-insensitive filesystems (macOS, Windows), you might encounter this error due to case differences. For example, if Git tracks Config.js but you have config.js locally:

bash
# Temporarily enable case-insensitive mode
git config core.ignoreCase true
git checkout target-branch
git config core.ignoreCase false

During interactive rebase:

If this error occurs during git rebase -i, you may need to abort and handle files first:

bash
git rebase --abort
git stash -u
git rebase -i HEAD~5
git stash pop

Sparse checkout alternative:

If you're working with a large repository and only need certain directories, consider using sparse checkout to avoid checking out problematic paths entirely.

Frequently asked questions

What does "error: The following untracked working tree files would be overwritten" mean?
When you run git checkout, git switch, or git rebase, Git needs to update your working directory to match the target commit or branch. If the target contains files that also exist in your working directory as untracked files (files not yet added to Git), Git will refuse to proceed. This is a safety mechanism.
How do I fix "error: The following untracked working tree files would be overwritten"?
Fix Git's "untracked working tree files would be overwritten by checkout" error. Move, remove, or commit the conflicting files, then retry the checkout or rebase.
What causes "error: The following untracked working tree files would be overwritten"?
You created new files locally that also exist in the target branch Build artifacts or generated files match files tracked in another branch A colleague added files to a branch that you also created locally
Is the "error: The following untracked working tree files would be overwritten" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading