Lessons · Git · deleting a file git tracks
Removing a file properly
git rm deletes the file and stages the deletion in one step. git rm --cached stops tracking a file but leaves it on disk.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Deleting with plain rm leaves Git reporting the file as missing, unstaged. And the file committed by mistake, a secret or a build artefact, needs --cached: untracked, not deleted.
How to think about it
Gone from the project: git rm. Stays on disk but out of Git: git rm --cached, then add it to .gitignore so it does not come back as untracked.
Worked example
git rm old.txtDeletes the file and stages the deletion in one step.
git status --shortD old.txt: the removal is ready to commit.
git rm --cached secret.envStops tracking the file but leaves it on disk.
Your turn
Stop tracking a file but keep it on disk.
git rm secret.env
Try it in a real repository
The trap
A secret committed once is in history even after git rm. Rotate the secret; removing the file does not remove the commits that contain it.