Git Rm --cached: Stop Tracking a Local File
A tracked file is not made untracked by an ignore rule
The untrack fixture already has a committed generated.txt and a newly written .gitignore with /generated.txt. A tracked path remains tracked even if a rule matches it. Inspect the index before changing it.
git ls-files -- generated.txtgit check-ignore -v --no-index -- generated.txtThe first lists generated.txt, proving it is in the index. The second diagnoses the ignore pattern without consulting the index. Ordinary check-ignore omits tracked paths.
Remove only the index entry
First ensure the local copy is backed up if it matters and inspect staged and unstaged changes. In this clean synthetic file fixture, preview git rm --cached; it stages removal while leaving the working file intact. If Git refuses because content differs, inspect and preserve the versions; never force blindly.
git rm -n --cached -- generated.txtgit rm --cached -- generated.txtgit add -- .gitignoregit diff --cached --name-statusgit commit -m "Stop tracking generated output"The staged diff shows the new ignore policy and deletion of generated.txt. After commit the local file still exists; it is ignored and absent from the current commit. Earlier commits still contain it. Collaborators integrating the deletion can lose their clean tracked copy, so coordinate before removing shared paths.
Verify the result
Check the local copy and current index after committing. The next command reads a historical snapshot without replacing anything. If a real credential was ever committed, rotate or revoke it promptly and follow an incident recovery process; ignoring the path does not remove copies from Git history, clones or backups.
git ls-files -- generated.txtgit status --short --ignoredgit show HEAD~1:generated.txtThe first prints nothing; status shows !! generated.txt; the last displays the old committed sample. Practice: explain how ordinary git rm would differ.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Rm --cached: Stop Tracking a Local File — knowledge check
Apply the file management and ignore rules taught in this lesson.
What happens if a tracked generated.txt matches a new ignore rule?
Which command stages removal but leaves the local file?
What does git ls-files -- generated.txt print after removal from the index?
Does committing the staged removal delete every historical copy?
A real token was committed. What is the priority?