Skip to main content
Module 1 · Lesson 5 of 55/34 in the course~15 min
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:

CommandRemoves from Disk?Stages in Index?Typical Use Case
rm fileYesNo (shows unstaged deletion)Shell-only delete before staging
git rm fileYesYes (stages deletion)Decommission an obsolete file
git rm --cached fileNo (keeps file)Yes (stages untrack)Remove secret/cache from Git cleanly
git mv old newMoves old → newYes (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.

Bash
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.

Bash
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!

Bash
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.

Bash
git rm --cached README.md
echo "README.md" >> .gitignore
git add .gitignore
git commit -m "chore: untrack personal README from repo"
Practice Stage

Step 1 — Rename a tracked module cleanly

Exercise#git.m1.l5.e1
Attempts: 0Loading…

Move src/tax.ts to src/vat.ts using git mv and verify the Staging Index with git status before committing.

Loading editor…
Show hint

Run git mv src/tax.ts src/vat.ts followed by git status.

Solution available after 3 attempts

Practice Stage

Step 2 — Rename and remove tracked paths in one clean commit

Exercise#git.m1.l5.e2
Attempts: 0Loading…

Rename src/tax.ts to src/vat.ts with git mv, delete README.md cleanly using git rm, and commit both snapshot changes cleanly.

Loading editor…
Show hint

Run git mv src/tax.ts src/vat.ts, git rm README.md, and git commit -m "...".

Solution available after 3 attempts

Practice Stage

Step 3 — Untrack one file cleanly while renaming another

Exercise#git.m1.l5.e3
Attempts: 0Loading…

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.

Loading editor…
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

Git Quiz#git.m1.l5.e4
Ready

Your teammate ran rm config.ts in the terminal without git, then committed without staging config.ts. Why is config.ts still inside history?

Answer options
Git Quiz#git.m1.l5.e5
Ready

Which sequence renames src/old.ts to src/new.ts cleanly AND preserves its previous commit history in git log best?

Answer options