Module lessons (5/5)
Deleting & renaming tracked files safely
Why Not Use Plain Shell rm and mv?
Running OS rm or mv updates only the Working Tree, leaving the old filename staged or listed as unstaged deletion plus an untracked new path until you stage both ends manually.
Git provides dedicated porcelain shortcuts — git rm and git mv — that update disk and the Staging Index atomically in one command:
| Command | Removes from Disk? | Stages in Index? | Typical Use Case |
|---|---|---|---|
rm file | Yes | No (shows unstaged deletion) | Shell-only delete before staging |
git rm file | Yes | Yes (stages deletion) | Decommission an obsolete file |
git rm --cached file | No (keeps file) | Yes (stages untrack) | Remove secret/cache from Git cleanly |
git mv old new | Moves old → new | Yes (stages removal + add) | Rename or move source modules |
Watch Staging Updates in the Four Areas
Notice how every file delete or move updates the Staging Index ready for the next immutable snapshot commit:
Example 1 — Deleting a Tracked File with git rm
git rm removes the file from your workspace directory AND queues the deletion for your upcoming commit.
git rm legacy-notes.md
git status # Changes to be committed: deleted: legacy-notes.md
git commit -m "chore: drop obsolete notes file"Example 2 — Renaming a Module with git mv
git mv moves the source file cleanly on disk and stages both sides in the Staging Index so history continuity stays intact.
git mv src/tax.ts src/vat.ts
git status # Staged deletion of src/tax.ts + new src/vat.ts
git commit -m "refactor: rename tax helper to vat"Example 3 — What Happens If You Used Shell mv Instead
Forgot to prefix git? Stage both the removed old path and the created new path (e.g., via git add -A or git add old new) — Git sees the identical blob SHA!
mv src/auth.ts src/session.ts
git add -A src/auth.ts src/session.ts
git status # Now staged identically to git mv!Example 4 — Untracking Docs Locally with git rm --cached
Need to keep a local developer checklist (TODO.md) on disk while dropping it from shared repository commits? Combine --cached with .gitignore.
git rm --cached README.md
echo "README.md" >> .gitignore
git add .gitignore
git commit -m "chore: untrack personal README from repo"Step 1 — Rename a tracked module cleanly
Move src/tax.ts to src/vat.ts using git mv and verify the Staging Index with git status before committing.
Show hint
Run git mv src/tax.ts src/vat.ts followed by git status.
Solution available after 3 attempts
Step 2 — Rename and remove tracked paths in one clean commit
Rename src/tax.ts to src/vat.ts with git mv, delete README.md cleanly using git rm, and commit both snapshot changes cleanly.
Show hint
Run git mv src/tax.ts src/vat.ts, git rm README.md, and git commit -m "...".
Solution available after 3 attempts
Step 3 — Untrack one file cleanly while renaming another
Use git rm --cached README.md so README.md remains on disk while removed from Git, rename src/auth.ts to src/session.ts with git mv, add README.md to .gitignore, and commit everything cleanly.
Show hint
Run git rm --cached README.md, echo README.md into .gitignore, git mv src/auth.ts src/session.ts, git add .gitignore, then commit.
Solution available after 3 attempts
Your teammate ran rm config.ts in the terminal without git, then committed without staging config.ts. Why is config.ts still inside history?
Which sequence renames src/old.ts to src/new.ts cleanly AND preserves its previous commit history in git log best?