This error occurs when you try to remove a file with git rm that has uncommitted changes. Git blocks the removal to prevent accidental data loss. Use --cached to keep the file or -f to force removal.
When you run `git rm` on a file, Git performs a safety check to ensure the file in your working directory matches the version in the current HEAD commit. If the file has been modified but not committed, Git refuses to remove it and displays this error. This is a deliberate safety feature, not a bug. The files being operated on must be identical to the files in the current HEAD. If there is a discrepancy between the HEAD version and the working tree version, Git blocks the removal to prevent you from accidentally losing uncommitted work. The error message helpfully suggests two options: use `--cached` to keep the file on disk but remove it from Git tracking, or use `-f` to force removal (which permanently deletes your local changes).
First, understand what changes exist in the file:
git statusThis shows whether the file is modified, staged, or both. To see the actual changes:
git diff file.txtThis helps you decide whether to save the changes or discard them.
If you want to preserve your changes in the Git history before removing the file:
git add file.txt
git commit -m "Save final version of file.txt before removal"
git rm file.txt
git commit -m "Remove file.txt"This creates a clean history showing the file's final state.
If you want to stop tracking the file in Git but keep it on your local filesystem:
git rm --cached file.txtThis removes the file from the Git index (staging area) but leaves it in your working directory. The file will appear as untracked after this command.
Common use case: Adding a file to .gitignore that was previously tracked:
echo "file.txt" >> .gitignore
git rm --cached file.txt
git commit -m "Stop tracking file.txt"If you want to remove the file and discard your local changes permanently:
git rm -f file.txtWARNING: This permanently deletes your uncommitted changes with no way to recover them. Make sure you don't need those changes before using this option.
The -f flag overrides Git's safety check that normally prevents removal of files with local modifications.
Before removing files (especially with wildcards), use the dry-run option to see what would be removed:
git rm -n file.txt
git rm --dry-run "*.log"This outputs which files would be removed without actually deleting anything. Especially important when using glob patterns.
If you're unsure whether you need the changes, stash them first:
git stash push -m "backup before removal" file.txt
git rm file.txtLater, if you need the changes back:
git stash popThis provides a safety net while still allowing you to proceed with the removal.
If you ran git rm by mistake (without -f) and want to restore the file:
git restore --staged file.txt
git restore file.txtOr using the older syntax:
git reset HEAD file.txt
git checkout -- file.txtNote: If you used git rm -f on a file with uncommitted changes, those changes are lost permanently.
### Understanding --cached vs -f
| Flag | Removes from Index | Removes from Disk | Local Changes |
|------|-------------------|-------------------|---------------|
| --cached | Yes | No (file stays) | Preserved in working directory |
| -f | Yes | Yes | Permanently lost |
Choose --cached when you want to untrack a file but keep it locally (e.g., config files, build artifacts).
Choose -f only when you're certain you don't need the uncommitted changes.
### Removing Multiple Files
When removing multiple files, Git checks each one. If any file has local modifications, the entire operation fails. Use wildcards carefully:
# Safe: preview first
git rm -n "*.tmp"
# Force remove all matching files
git rm -f "*.tmp"### Staged vs Working Directory Changes
Git's safety check considers both:
- Staged changes: Content in the index differs from HEAD
- Working directory changes: Content on disk differs from HEAD
If you have staged changes you want to keep, commit them first. The --cached flag still works if staged content differs from HEAD.
### Recursive Directory Removal
To remove a directory and all its contents:
# Will fail if any file has local modifications
git rm -r directory/
# Force remove entire directory
git rm -rf directory/Be extra careful with -rf on directories as it bypasses all safety checks.
### Why Git Has This Safety Feature
The git rm command stages a file deletion for the next commit. If Git allowed removing modified files without warning, you could easily lose work that hasn't been committed anywhere. Unlike committed changes (which can be recovered from Git history), uncommitted local changes exist only on your disk.
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