Remove file from the repository but keep it locally
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 still keep that file on your machine, use git rm --cached. The --cached flag removes the path from the Git index only, so the next commit deletes it from the repository while leaving your working copy untouched.
Use git rm --cached
Git tracks files through the index. A normal git rm path/to/file removes the file from both the index and the working tree. Adding --cached changes that behavior so only the tracked entry is removed.
After that commit, the repository no longer tracks the file, but the file still exists on disk in your local clone. Adding the path to .gitignore is usually the second required step, because otherwise Git will immediately show the file again as untracked.
This workflow is useful for local configuration, machine-specific secrets, generated files, IDE settings, or anything else that was committed once but should not stay under version control.
Removing a Directory but Keeping It Locally
The same idea works for directories. Use the recursive flag when the tracked path contains multiple files:
That pattern is common for editor metadata, local cache folders, and build output that should remain on a developer machine but not in the shared repository.
What Happens to Repository History
git rm --cached only changes the current state of the branch. It does not erase the file from earlier commits. If someone checks out an older commit, the file can still be present there.
That distinction matters a lot when the file contains sensitive data. If an API key, password, private certificate, or token was committed, removing it from the current branch is not enough. You should rotate the secret and then decide whether the repository history also needs to be rewritten.
A good mental model is:
- '
git rm --cachedstops future tracking' - history cleanup handles old commits
Those are related tasks, but they are not the same operation.
Verifying the Result
Before you commit, check both Git and the filesystem:
git status should show the file as deleted from version control. The ls command confirms that the local file still exists.
After the commit, run git status again. If the file keeps appearing as untracked, your ignore rule is missing or does not match the correct path.
Shared Ignore Rules Versus Local Ignore Rules
Sometimes the ignore pattern should be shared with the whole team. In that case, put it in .gitignore. Other times the rule is personal and should stay only in your clone. Then .git/info/exclude is the better choice.
Use .gitignore for files that everyone should ignore. Use .git/info/exclude for personal scratch files that only matter on your machine.
Common Pitfalls
The most common mistake is forgetting --cached. Without it, Git deletes the working copy as well as the tracked entry.
Another issue is removing the file from the index but forgetting to add an ignore rule. The file stays local, but Git continues to show it as untracked after every status check.
Developers also assume this command removes the file from history. It does not. If secrets were committed, handle secret rotation and possibly history cleanup separately.
Summary
- Use
git rm --cachedto stop tracking a file while keeping it locally. - Add the path to
.gitignoreor.git/info/excludeso it does not keep reappearing in status output. - Use
git rm -r --cachedwhen the tracked path is a directory. - The command affects future commits only and does not erase older history.
- If the file contained secrets, rotate them and evaluate whether the repository history must also be cleaned.
Related reading
- Remove file from Version Control in IntelliJ IDEA
- Remove folder and its contents from Git/GitHub's history
- Remove large .pack file created by Git
- Remove last commit from remote Git repository
- Remove local git tags that are no longer on the remote repository
- Remove refs/original/heads/master from git repo after filter-branch --tree-filter?
- Remove sensitive files and their commits from Git history
- Remove specific commit
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.