Hone

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.txt
Deletes the file and stages the deletion in one step.
git status --short
D old.txt: the removal is ready to commit.
git rm --cached secret.env
Stops tracking the file but leaves it on disk.

Your turn

Stop tracking a file but keep it on disk.

git rm  secret.env

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.

Practise deleting a file git tracks on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.