git
git rm
version control
file management
command line

How do I git rm a file without deleting it from disk?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If you want Git to stop tracking a file but keep the file on your machine, use git rm --cached. The --cached flag removes the path from the Git index only, which means the file stays on disk while Git treats it as no longer tracked.

This is the usual fix when you accidentally committed a generated file, a local config file, or some other file that should exist locally but should not remain under version control. It is also the right move when a file belongs to each developer's machine but not to the shared repository state.

The Exact Command

For a single file:

bash
git rm --cached path/to/file

After that:

  • the file still exists in your working directory
  • Git stages its removal from the repository
  • the next commit records that the file is no longer tracked

You can verify the staged change with:

bash
git status

If the goal is permanent untracking, commit the change afterward.

Add It to .gitignore

If you stop tracking the file but do not ignore it, Git will show it as untracked later. Usually you also want to add the path or pattern to .gitignore.

Example:

gitignore
.env
local.settings.json
build/

Then run:

bash
git rm --cached .env
git add .gitignore
git commit -m "Stop tracking local environment file"

That combination removes the file from version control and prevents it from being re-added accidentally.

Remove a Directory Without Deleting It Locally

For directories, add -r:

bash
git rm -r --cached build/

This recursively removes tracked entries from the index while keeping the directory contents on disk.

That is common for build artifacts, dependency folders that should not be committed, or generated documentation outputs.

What Actually Changes

Git tracks file content through the index and commits, not by owning the file on disk. git rm --cached updates the index so Git stops including that path in future commits.

It does not mean the file disappears for everyone immediately. Other collaborators will see the removal once they pull the commit that stops tracking it. If the file was already committed, its history still exists in old commits unless you rewrite history separately.

That distinction matters for secrets. If you accidentally committed a secret, git rm --cached stops future tracking, but it does not erase the secret from repository history.

When You Need More Than --cached

If the file was committed by mistake and contains sensitive data, the steps are usually:

  1. remove it from tracking with git rm --cached
  2. add it to .gitignore
  3. rotate the secret
  4. optionally rewrite Git history if exposure matters

Do not confuse "untracked from now on" with "removed from history."

Common Pitfalls

  • Using git rm without --cached, which deletes the file from disk as well as from the index.
  • Forgetting to add the file to .gitignore, so it reappears as untracked.
  • Expecting git rm --cached to erase a file from past commits.
  • Omitting -r when trying to untrack a directory.

Summary

  • Use git rm --cached path/to/file to stop tracking a file without deleting it locally.
  • Add the file to .gitignore if it should stay untracked in the future.
  • Use git rm -r --cached for directories.
  • The command removes the path from the Git index, not from your working directory.
  • If the file contained secrets, remember that stopping tracking does not remove it from repository history.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions