This error occurs when Git tries to switch branches or perform a rebase, but detects that untracked files in your working directory would be overwritten by files from the target branch. Git refuses to proceed to prevent accidental data loss.
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.
First, examine the files Git lists in the error message. Determine if they contain important work you want to keep:
# 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.localDecide whether these files contain work you need to preserve or if they can be safely removed.
If you want to preserve the untracked files and restore them later, use git stash with the -u flag to include untracked files:
# 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 popNote: git stash pop may cause merge conflicts if the restored files conflict with the branch content.
If the files are not important (e.g., build artifacts, temporary files), you can remove them with git clean:
# 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-branchWarning: git clean permanently deletes files. Always run with -n (dry run) first to verify.
If these files represent work that should be part of your repository, commit them first:
# 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-branchThis converts the conflict from an untracked file issue to a normal merge conflict, which Git can handle.
For maximum safety, manually move the files out of the repository:
# 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.
If you're certain you don't need the untracked files, you can force the checkout:
# Force checkout - WILL DELETE untracked files that conflict
git checkout -f target-branch
# Or with git switch (Git 2.23+)
git switch -f target-branchWarning: This permanently deletes the conflicting untracked files. Only use this if you're certain the files are not needed.
Preventing future occurrences:
Use a .gitignore file to prevent build artifacts, IDE files, and environment-specific files from becoming a source of conflicts:
# Build outputs
dist/
build/
node_modules/
# IDE files
.idea/
.vscode/
*.swp
# Environment files
.env.local
.env.*.localCase 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:
# Temporarily enable case-insensitive mode
git config core.ignoreCase true
git checkout target-branch
git config core.ignoreCase falseDuring interactive rebase:
If this error occurs during git rebase -i, you may need to abort and handle files first:
git rebase --abort
git stash -u
git rebase -i HEAD~5
git stash popSparse 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.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server