git rm: Usage, Options & Common CI Errors
git rm deletes files and stages the removal in one step.
Use git rm to delete tracked files; --cached untracks a file while keeping it on disk.
What it does
git rm removes files from both the working tree and the index, staging the deletion for the next commit. With --cached it removes only from the index, leaving the file in place.
Common usage
Terminal
git rm file.txt
git rm -r dir/ # recursive
git rm --cached secrets.env # stop tracking, keep file
git rm -f file.txt # force when modified/stagedOptions
| Flag | What it does |
|---|---|
| --cached | Remove from index only, keep on disk |
| -r | Recurse into directories |
| -f / --force | Override the up-to-date safety check |
| -n / --dry-run | Show what would be removed |
Common errors in CI
error: the following file has staged content different from both the file and the HEAD / 'X' has changes staged in the index - git protects unsaved changes. Use -f to force, or git rm --cached to merely untrack (commonly after adding a path to .gitignore).
Related guides
git add: Usage, Options & Common CI Errorsgit add stages changes for the next commit. Reference for -A, -p, --update, and the pathspec and ignored-file…
git mv: Usage, Options & Common CI Errorsgit mv moves or renames a tracked file and stages the change. Reference for -f, -k, and the "destination exis…
git clean: Usage, Options & Common CI Errorsgit clean removes untracked files from the working tree. Reference for -n, -f, -d, -x, and the "would clobber…
git restore: Usage, Options & Common CI Errorsgit restore discards working-tree changes or unstages files. Reference for --staged, --source, --worktree, an…