What is the difference between git rm and just deleting a file?
Understand the difference between git rm and manually deleting a file, plus git rm --cached to untrack files, with clear examples and interview answers.
Expected Interview Answer
'git rm' removes a file from both the working directory and the staging index in one step, so the deletion is already staged for the next commit; deleting a file manually only removes it from the working directory and leaves the change unstaged until you run 'git add' (or 'git rm') to record it.
After a plain 'rm' or file-manager delete, git shows the file as 'deleted' but not staged, and you must still stage that deletion before committing. 'git rm' does both actions together. Variants matter too: 'git rm --cached' unstages and stops tracking a file while keeping it on disk, and 'git rm -f' is needed to remove a file with staged changes.
- 'git rm' stages the deletion in a single command
- Avoids forgetting to 'git add' the removal before committing
- 'git rm --cached' stops tracking a file without deleting it
- Makes intent explicit and keeps the index consistent
- Handles directories with '-r' and forced removals with '-f'
AI Mentor Explanation
Deleting a file by hand is like a batter walking off the pitch without telling the scorer — the official scorecard still lists them as in play until someone updates it. 'git rm' is like the batter being formally given out and the scorer recording it in the same moment, so the book and the field agree instantly. One leaves the record trailing reality; the other keeps the record and the field in sync at once.
Step-by-Step Explanation
Step 1
Understand plain deletion
Running 'rm file' or deleting in a file manager removes it from disk but leaves the change unstaged.
Step 2
Check the status
'git status' shows the file as 'deleted' and not staged for commit.
Step 3
Stage or use git rm
Either 'git add file' to stage the deletion, or run 'git rm file' to delete and stage together.
Step 4
Use --cached to untrack
'git rm --cached file' stops tracking the file but keeps it on disk — common for gitignoring.
Step 5
Commit the removal
Commit so the deletion (or untracking) is recorded in history.
What Interviewer Expects
- Knowing git rm updates working tree and index together
- Knowing plain delete leaves the change unstaged
- Understanding 'git rm --cached' keeps the file but untracks it
- Awareness of '-f' for forced removal and '-r' for directories
- Correct 'git status' interpretation after each
Common Mistakes
- Thinking a manual delete is automatically committed
- Forgetting to stage a manual deletion before committing
- Confusing 'git rm --cached' with a full delete
- Not using '-r' when removing a tracked directory
- Expecting 'git rm' to work without '-f' when there are staged changes
Best Answer (HR Friendly)
“'git rm' deletes a file and records that deletion in one step, so git already knows about it. Just deleting a file removes it from your folder but you still have to tell git separately, which is easy to forget.”
Code Example
# Manual delete: removes from disk only, change is unstaged
rm notes.txt
git status # shows: deleted: notes.txt (not staged)
git add notes.txt # you must stage it yourself
# git rm: removes from disk AND stages the deletion at once
git rm notes.txt
git status # shows: deleted: notes.txt (staged)git rm --cached secrets.env
echo "secrets.env" >> .gitignore
git commit -m "Stop tracking secrets.env"
# secrets.env still exists locally, just no longer trackedFollow-up Questions
- What does 'git rm --cached' do and when is it useful?
- How do you remove a whole tracked directory with git?
- Why might 'git rm' require the '-f' flag?
- How do you recover a file you deleted and committed?
- How does .gitignore interact with already-tracked files?
MCQ Practice
1. What does 'git rm file.txt' do that a plain 'rm file.txt' does not?
'git rm' removes the file and stages the deletion, whereas plain 'rm' leaves the deletion unstaged.
2. Which command stops tracking a file but keeps it on disk?
'git rm --cached' removes the file from the index only, so it becomes untracked but remains in the working directory.
3. After manually deleting a tracked file, 'git status' shows it as:
A manual delete leaves the change unstaged, so git reports the file as deleted and awaiting staging.
Flash Cards
What does git rm do? — Deletes the file from the working tree and stages the deletion in the index together.
Plain rm vs git rm? — Plain rm deletes from disk but leaves the change unstaged; git rm deletes and stages in one step.
What does git rm --cached do? — Untracks the file (removes it from the index) while keeping it on disk.
When is git rm -f needed? — When the file has staged changes that differ, git requires -f to force removal.