How to Remove Files from Git Staging Area and Working Directory

In the last blog, we explored how .gitignore works across directories. This time, let’s see how to remove files from Git’s staging area or even from both the staging area and the working directory.

Sometimes we add a file by mistake — for example, a debug file, wrong configuration, or a test file that should not be tracked. Instead of panicking, Git provides an easy way to undo it.


Scenario: Adding a Wrong File

Imagine you create a file:

echo "This is a bug file" > wrongfile.txt

Now you run:

git add wrongfile.txt
git status -s

You see that wrongfile.txt is in the staging area (index). But you realize this file should not be there.


1. Remove File Only from Staging Area (Unstage)

If you only want to remove it from staging (but keep it in your working directory):

git rm --cached wrongfile.txt

👉 Result:

  • File goes back to untracked state.
  • File is still available locally in your directory.

2. Remove File from Staging + Working Directory

If you want the file to be removed completely (from staging and from your local directory):

git rm -f wrongfile.txt

👉 Result:

  • File is removed from staging area.
  • File is also deleted from your disk.

This is useful when you know the file is not needed at all.


3. Difference Between git rm and Linux rm

If you delete a file using Linux rm like this:

rm wrongfile.txt
  • File is deleted from your working directory.
  • But it still remains in Git’s staging area.

That means Git still thinks you want to track its deletion. So Git keeps the “removal” staged until you commit.

With git rm, Git takes care of both staging and working directory properly.


Key Learning

  • Use git rm --cached → remove only from staging (keep file locally).
  • Use git rm -f → remove from both staging and working directory.
  • Using Linux rm alone will not unstage the file — it just deletes it locally.

🚀 Don’t Miss Out!

Now you know how to safely remove files from Git’s staging area and working directory. In the next blog, we’ll learn how to recover files from Git after accidental removal.

👉 Subscribe to Learning Ocean – Subscribers get coupon codes, early access to blogs/courses, and exclusive YouTube videos.

👉 My YouTube Channel

👉 Watch this video explanation

Stay curious, keep coding, and let’s master Git together! 🎉