learn git.
0/25 done
learn git/everyday git/course 13 of 25

Deleting files the git way

Git saw everything you delete too. Deletions are part of the photograph.

6 min read·3 sections·3 tasks
the key idea

Deleting a file is just another change — it gets staged and committed like any edit.

01Two ways to delete

When you delete a file, git does not panic — it records the deletion as a change, exactly like an edit. You can delete with your normal shell rm: git status then shows the file under deleted, waiting to be staged, and you stage that deletion with git add — the same command as always, because staging a deletion is staging a change.

Or you do both moves in one command: git rm <file> deletes the file and stages the deletion together. It is pure shorthand — the result is identical to rm followed by add. Use whichever you remember; professionals use both daily.

[bash]· the long way and the short way, same result
rm old-report.txt
git add old-report.txt      # stage the deletion

# or, the shortcut:
git rm old-report.txt

02Deleting is recorded, not erased

Here is the part that surprises people pleasantly: after you commit a deletion, the file is gone from your folder but not from your history. Every old photograph still contains it, whole and complete. If you ever need the file back, git can fetch it out of any previous photograph — you will learn that machinery in a later course series.

This is why teams delete fearlessly. The album keeps everything; the working folder just reflects the latest photograph. Nothing that was ever committed is ever truly lost.

Git is a safety net for deletions too

Committed it once? Then deleted it? It is still recoverable. The command to bring files back is the one thing this simulator does not teach — but knowing it exists changes how bravely you can work.

03The special case: untracking a file

One more form worth knowing exists, for a very specific accident: you committed a file that should have been in .gitignore. git rm --cached <file> removes the file from git's tracking — stages its disappearance from the album — while leaving the file sitting untouched on your disk. From now on it is untracked, and with a matching .gitignore entry, invisible.

Read the flag as: remove from git's memory (--cached), not from my computer. It is the gentlest form of deletion git has.

[bash]· rescuing a committed secret: untrack it, then ignore it
git rm --cached settings.env
echo "settings.env" >> .gitignore
The old photographs still have it

--cached stops future commits from including the file, but every photograph taken before the rescue still contains it. For real secrets, change the password — scrubbing history is advanced surgery.

>>

Practice

Two files are committed. Delete both, the git way.

[practice]· a real terminal, safely simulated
practice terminal — everything here is simulated, nothing can break
type 'help' to see the commands this terminal understands
~/my-project (main) $
Your tasks0/3 done
  1. 01.Delete one.txt and stage the deletion.

    hint: rm one.txt then git add one.txt (or just: git rm one.txt)

  2. 02.Delete two.txt with git rm.

    hint: Try: git rm two.txt

  3. 03.Commit the deletions.

    hint: git commit -m "remove old files"