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.
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:
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:
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:
Then run:
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:
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:
- remove it from tracking with
git rm --cached - add it to
.gitignore - rotate the secret
- optionally rewrite Git history if exposure matters
Do not confuse "untracked from now on" with "removed from history."
Common Pitfalls
- Using
git rmwithout--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 --cachedto erase a file from past commits. - Omitting
-rwhen trying to untrack a directory.
Summary
- Use
git rm --cached path/to/fileto stop tracking a file without deleting it locally. - Add the file to
.gitignoreif it should stay untracked in the future. - Use
git rm -r --cachedfor 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.
Related reading
- How do I ignore an error on 'git pull' about my local changes would be overwritten by merge?
- How do I ignore files in a directory in Git?
- How do I install a specific version of Erlang/OTP?
- How do I list all remote branches in Git 1.7+?
- How do I list all the files in a commit?
- How do I make a branch point at a specific commit?
- How do I make a Git commit in the past?
- How do I make git-svn use a particular svn branch as the remote repository?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.