---
title: "How do I remove a file from Git but keep it locally?"
description: "Stop tracking a file in Git without deleting it from your disk. Learn how git rm --cached works and why adding a file to .gitignore does not untrack a file Git already tracks."
url: https://www.danny.engineering/answer/git/how-do-i-remove-a-file-from-git-but-keep-it-locally
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Git"]
---

# How do I remove a file from Git but keep it locally?

## The short answer


Use `git rm --cached`. It removes the file from Git's index, so the next commit stops tracking it, but leaves your copy on disk. Add it to `.gitignore` in the same commit so it does not get added again.


```bash
git rm --cached .env
echo ".env" >> .gitignore
git add .gitignore
git commit -m "Stop tracking .env"
```


## Why it happens


Git tracks a file once it has been added to the index and committed. From then on Git keeps watching it, whatever your ignore rules say.


`.gitignore` only affects untracked files. It tells `git add` and `git status` which new files to leave alone. It does not untrack files that are already in the repository. That is why adding `node_modules/` or `.env` to `.gitignore` after committing them seems to do nothing: Git still shows every change to them.


There are two different "remove" commands:

- `git rm file` removes the file from the index and deletes it from your working folder.
- `git rm --cached file` removes it from the index only. Your file stays where it is and becomes untracked.

## How to fix it


### Untrack a single file


```bash
git rm --cached config/local.json
echo "config/local.json" >> .gitignore
git add .gitignore
git commit -m "Stop tracking local config"
```


After the commit, `git status` no longer reports changes to the file, and it will not be pushed.


### Untrack a whole folder


Add `-r` to recurse into directories.


```bash
git rm -r --cached node_modules
echo "node_modules/" >> .gitignore
git add .gitignore
git commit -m "Stop tracking node_modules"
```


### Reapply .gitignore to everything


If you have just written or fixed a `.gitignore` and want every matching file untracked in one go, clear the index and add everything back. Files that match your ignore rules will not be re-added.


```bash
git rm -r --cached .
git add .
git status   # check the list of deleted files before committing
git commit -m "Apply .gitignore"
```


Review `git status` carefully. Anything listed as deleted will stop being tracked.


### Check why a file is or is not ignored


`git check-ignore` shows which rule matches a path. It returns nothing if no rule matches.


```bash
git check-ignore -v .env
# .gitignore:3:.env    .env
```


### Ignore a file only on your machine


For files that only you need to ignore, such as editor settings, add them to `.git/info/exclude` instead of `.gitignore`. It uses the same pattern format but is never committed. For rules you want in every repository, set a global ignore file with `core.excludesFile`.


## Common pitfalls

- Expecting the file to stay on everyone's machine. Your commit records the file as deleted, so when teammates pull it, Git deletes their copy too. Warn them, or ask them to back the file up first.
- Thinking a committed secret is now safe. `git rm --cached` only affects future commits; the file is still in every earlier commit and in any clone or fork. Rotate the secret straight away. Removing it from history needs a history rewrite, for example with the separate `git filter-repo` tool, plus a force push.
- Using `git rm` without `--cached`. That deletes your local file as well. If you did it by mistake before committing, `git restore --staged` and then `git restore` on the file bring it back.
- Reaching for `git update-index --assume-unchanged` or `--skip-worktree` to ignore a tracked file. The Git documentation says these are not designed for ignoring changes to tracked files, and they can lead to surprising results.
- Adding a folder pattern without the trailing slash. `build` matches files and folders named build anywhere; `build/` matches folders only.

## Related reading

- [git-rm documentation](https://git-scm.com/docs/git-rm)
- [gitignore documentation](https://git-scm.com/docs/gitignore)
