KKAIROS
Practical guideIllustrative4 min read

Git Rm Cached: stop tracking a file and keep it on disk

Stop tracking a file and keep it on disk: git rm --cached FILE removes it from the index, so the next commit no longer carries it.

Published:

Updated:

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.

How do I stop tracking a file without deleting it?

git rm --cached with the path. It untracks the file and leaves it on disk.

Is the file still there after the commit?

Yes. After rm --cached and a commit, git tracks no file and the file is still on disk.

Why does git rm --cached fail on a file git does not track?

There is no tracked entry to remove. git stops with a fatal error.

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 one

Command:

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 --porcelain

Recorded output:

tracked=0
on_disk=yes
D  f.txt
?? f.txt

Exit 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 one

Command:

git rm -q --cached f.txt; git commit -qm gone; echo tracked=$(git ls-files | wc -l); ls

Recorded output:

tracked=0
f.txt

Exit 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 one

Command:

git rm -q --cached nosuch.txt

Recorded output: none, standard output was empty.

Standard error from the same run:

fatal: pathspec 'nosuch.txt' did not match any files

Exit 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

TestWhat it checksExitVerdictLog sha256 (first 12)
TE-79-Arm --cached untracks a file but the file stays on disk0PASS81cc8141b582
TE-79-Bafter rm --cached and a commit, git tracks no file and the file is still on disk0PASSc7fc814dc412
TE-79-Cexpected non-zero exit: rm --cached on a path git does not track stops with a fatal error128PASSbcb6b6cab5fa

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