Lesson Completion
Back to course

git rm and git mv

Beginner
9 minutes4.7Git

The Hook (The "Byte-Sized" Intro)

Deleting a file with rm is fine for your OS — but Git has no idea it happened. Renaming with mv? Git sees it as "one file deleted, one new file created." Use git rm and git mv instead, and Git tracks the change properly — your history stays clean, blame works correctly, and nobody panics during code review.

📖 What is git rm and git mv?

These commands handle file deletion and renaming within Git's tracking system. They perform the filesystem operation AND stage the change in one step, keeping your repository history clean and consistent.

Conceptual Clarity

CommandWhat It DoesEquivalent Manual Steps
git rm file.txtDeletes the file AND stages the deletionrm file.txt + git add file.txt
git mv old.txt new.txtRenames/moves the file AND stages the changemv old.txt new.txt + git add new.txt + git add old.txt
git rm --cached file.txtRemoves from tracking but keeps the file on diskUntrack only (useful for secrets accidentally committed)

Key insight: Git doesn't have a native "rename" concept. Internally, git mv is a delete + add. Git detects renames by comparing file contents using a similarity index (files >50% similar = rename).

Real-Life Analogy

  • git rm = Removing a book from the library catalog AND from the shelf in one step. Just throwing the book away (plain rm) leaves a ghost entry in the catalog.
  • git mv = Moving a book to a new shelf and updating the catalog simultaneously. Just moving the book (plain mv) confuses the librarian.

Visual Architecture

flowchart LR subgraph FILE_OPS["File Operations"] A["📄 old.txt"] -->|"git rm"| B["❌ Deleted + Staged"] A -->|"git mv"| C["📄 new.txt + Staged"] end subgraph CACHED["Keep on Disk"] D["📄 secret.env"] -->|"git rm --cached"| E["📄 Untracked<br/>(still on disk)"] end style FILE_OPS fill:#1a1a2e,stroke:#e94560,color:#e94560 style CACHED fill:#0f3460,stroke:#ffd700,color:#ffd700

Why It Matters

  • Clean history: git mv preserves rename detection, so git log --follow can track a file across renames.
  • One step: Both commands do the filesystem change + staging in a single command.
  • Untracking secrets: git rm --cached removes a file from Git without deleting it — essential for accidentally committed .env files.
  • Code review clarity: Diffs show clean renames instead of confusing delete+add pairs.

Code

bash
# ─── Delete a tracked file ─── git rm old-feature.js git status # Output: deleted: old-feature.js (staged) git commit -m "Remove deprecated feature module" # ─── Rename/move a file ─── git mv utils.js helpers.js git status # Output: renamed: utils.js -> helpers.js (staged) git commit -m "Rename utils to helpers for clarity" # ─── Move a file to a different directory ─── git mv config.json src/config/config.json git commit -m "Move config to src/config directory" # ─── Untrack a file but keep it on disk ─── git rm --cached .env echo ".env" >> .gitignore git add .gitignore git commit -m "Remove .env from tracking, add to gitignore" # The .env file still exists locally but Git no longer tracks it # ─── Force delete a modified, staged file ─── git rm -f modified-file.js # Use -f when the file has staged changes Git wants to protect # ─── Remove an entire directory ─── git rm -r old-tests/ git commit -m "Remove old test directory" # ─── Track renames through history ─── git log --follow --oneline helpers.js # Shows full history including before the rename

Common Scenarios

ScenarioCommand
Delete a filegit rm file.txt
Rename a filegit mv old.txt new.txt
Move to a directorygit mv file.txt dir/file.txt
Untrack without deletinggit rm --cached file.txt
Delete a directorygit rm -r directory/
Force-delete modified filegit rm -f file.txt

Key Takeaways

  • Use git rm and git mv instead of plain rm/mv — they stage the change automatically.
  • git rm --cached untracts a file without deleting it from disk — essential for secret files.
  • Git detects renames by content similarity, not by filename — git mv helps ensure clean detection.
  • Use git log --follow to trace a file's history across renames.

Interview Prep

  • Q: What is the difference between rm file.txt and git rm file.txt? A: rm only deletes the file from the filesystem. git rm deletes the file AND stages the deletion for the next commit. With plain rm, you'd still need to run git add file.txt to stage the change.

  • Q: How do you stop tracking a file without deleting it? A: Use git rm --cached <file>. This removes the file from Git's index (staging area) but leaves it on disk. You should then add the file to .gitignore to prevent re-tracking.

  • Q: Does Git natively track renames? A: No. Git has no explicit rename concept. It detects renames after the fact by comparing file contents — if a deleted file and a new file share >50% similarity, Git reports it as a rename. git mv performs the delete+add cleanly to help this detection.

Topics Covered

Git BasicsGit Fundamentals

Tags

#git#rm#mv#files

Last Updated

2026-02-12