Git Rm Cached: stop tracking a file and keep it on disk
git rm --cached FILE removes the file from the index, so Git stops tracking it, and leaves the file in the working tree.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-79-A: rm --cached untracks a file but the file stays on disk
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm oneCommand:
git rm -q --cached f.txt; echo tracked=$(git ls-files f.txt | wc -l); echo on_disk=$([ -f f.txt ] && echo yes || echo no); git status --porcelainRecorded output:
tracked=0
on_disk=yes
D f.txt
?? f.txtExit status: 0. The output matches the expected text written before the run.
TE-79-B: after rm --cached and a commit, git tracks no file and the file is still on disk
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm oneCommand:
git rm -q --cached f.txt; git commit -qm gone; echo tracked=$(git ls-files | wc -l); lsRecorded output:
tracked=0
f.txtExit status: 0. This status and the output match what was written before the run.
TE-79-C: expected non-zero exit: rm --cached on a path git does not track stops with a fatal error
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm oneCommand:
git rm -q --cached nosuch.txtRecorded output: none, standard output was empty.
Standard error from the same run:
fatal: pathspec 'nosuch.txt' did not match any filesExit status: 128. This status and the output match what was written before the run.
TE-79-B: after git rm --cached and a commit, git ls-files lists no file, so tracked=0, and ls still shows f.txt. TE-79-C: git rm --cached nosuch.txt exited with status 128, and standard error reads fatal: pathspec 'nosuch.txt' did not match any files.
Limits
The run uses one file. Removing a directory (-r) and the effect on other clones were not tested. Not tested on this page: -n (only simulate the removal), -f (override the up-to-date check), --ignore-unmatch (exit with a zero status even when nothing matched), --pathspec-from-file (read the pathspec from a file) and --pathspec-file-nul (separate the pathspec elements with a NUL character). The names and one-line descriptions come from git rm -h in git 2.34.1. No git rm command here used them.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-79-A | rm --cached untracks a file but the file stays on disk | 0 | PASS | 81cc8141b582 | | TE-79-B | after rm --cached and a commit, git tracks no file and the file is still on disk | 0 | PASS | c7fc814dc412 | | TE-79-C | expected non-zero exit: rm --cached on a path git does not track stops with a fatal error | 128 | PASS | bcb6b6cab5fa |
Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides