Coding

How to Remove a File From a Git Repo but Keep It Locally

Run git rm --cached path/to/file, add the file to .gitignore, and commit: Git stops tracking the file but leaves your local copy on disk untouched. For a folder, add -r: git rm -r --cached build/.

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

What git rm --cached actually does

Git keeps three copies of a tracked file in play: the version in the last commit, the version in the index (the staging area), and the file in your working folder. Plain git rm removes the file from the index and deletes it from disk. The --cached flag, in the words of the git-rm manual, removes paths "only from the index. Working tree files, whether modified or not, will be left alone."

Example output from a test repository (Git 2.45):

$ git rm --cached .env
rm '.env'
$ git status --short
D  .env
?? .env

The two lines look contradictory but are exactly right. D in the first column means "deletion staged for the next commit". ?? means the file now exists on disk as an untracked file. ls -a still shows .env; nothing was deleted locally.

Why .gitignore alone doesn't work

Adding a tracked file to .gitignore does nothing on its own. The gitignore manual says plainly: "Files already tracked by Git are not affected." Ignore rules only stop untracked files from being added. The documented fix is the two-step combination in this article: git rm --cached to stop tracking, then the .gitignore entry so the file isn't added back by the next git add ..

If you skip the .gitignore step, the file sits in git status as untracked, and the next git add -A commits it again.

How to remove a folder from the repo but keep it locally

git rm -r --cached build/
echo "build/" >> .gitignore
git add .gitignore
git commit -m "Stop tracking build output"

Without -r, Git refuses with fatal: not removing 'build' recursively without -r. With it, Git prints one rm '...' line per file it stops tracking, such as rm 'build/out.js'.

To preview what would be removed without changing anything, add -n (dry run): git rm -r -n --cached build/.

How to apply a new .gitignore to files already committed

When you add several patterns at once (say dist/, *.log and .env), you can untrack everything and re-add it, letting .gitignore filter the result:

git rm -r --cached .
git add .
git status --short

Check git status before committing. Files now matched by .gitignore show as D (removed from the repo, still on disk). Be aware that git add . also stages any untracked files that aren't ignored. In a test run, an unrelated scratch file appeared as A notes.txt alongside the intended D dist/a.js. Unstage anything you didn't mean to add with git restore --staged <file>, then commit.

What happens to teammates' copies when they pull

This is the part that surprises people. Your commit records the file as deleted from the project, and Git applies that deletion everywhere the commit goes. In a test with two clones of the same repository, the second clone had .env before pulling and did not have it after git pull. Only your copy survives, because --cached kept it.

So before you push:

  • Tell people to copy the file somewhere outside the repository before they pull, then copy it back afterwards. Because the file is now ignored, it stays put from then on.
  • For config files, commit a template instead, such as .env.example or config.example.json, and document "copy it to .env". The git-update-index manual recommends exactly this pattern: commit a sample file and have each person copy it to an ignored name.

The file is still in Git history

git rm --cached only affects commits from now on. Every earlier commit still contains the file, and anyone with the repository can read it. In the same test, after the removal commit, git show HEAD~1:.env still printed the old contents.

If the file contained a password, API key or token, treat it as leaked. GitHub's guidance is that "as a first step you need to revoke and/or rotate that secret". Rotate it at the provider first; rewriting history comes second and may not be needed. If you do need to purge it from history, GitHub's documentation uses git-filter-repo, which rewrites every commit and requires everyone to re-clone or rebase. If you only just committed the file and haven't pushed, the cheaper fix is to rewrite that one commit: see how to undo the last commit.

Why not use assume-unchanged or skip-worktree?

You'll see git update-index --assume-unchanged file and --skip-worktree suggested as a way to "keep a file but ignore my changes to it". The Git manual warns against this: "Users often try to use the assume-unchanged and skip-worktree bits to tell Git to ignore changes to files that are tracked. This does not work as expected." Git may still check the working file against the index during some operations, and with assume-unchanged it fails when an incoming commit changes the file, leaving you to sort it out by hand. Its own conclusion is that "Git does not provide a way to ignore changes to tracked files". Use git rm --cached plus a template file.

If git rm --cached fails

  • fatal: pathspec 'notes.txt' did not match any files: the file isn't tracked, so there's nothing to remove. Check the path and case with git ls-files | grep notes; if it's untracked, just add it to .gitignore.
  • error: the following file has staged content different from both the file and the HEAD followed by (use -f to force removal): you staged changes, then edited the file again. Git is protecting the staged version. Adding -f is safe here when combined with --cached, because the working file is still left alone; only the staged changes are discarded.
  • fatal: not removing 'build' recursively without -r: add -r for folders.

Frequently asked questions

Does git rm --cached delete the file from my computer?
No. --cached removes the file only from Git's index (the staging area). The file on disk is left alone, whether or not it has local changes. It shows up as untracked afterwards, until you add it to .gitignore.
Why is my file still tracked after I added it to .gitignore?
.gitignore only affects files Git is not already tracking. Once a file has been committed, Git keeps tracking it regardless of .gitignore. Run git rm --cached on it and commit; from then on the .gitignore rule keeps it out.
Will the file be deleted for other people when they pull?
Yes. Your commit records the file as deleted, so when a teammate pulls it, Git removes the file from their working folder too. Warn them first so they can copy it somewhere safe, or keep a template such as config.example.json in the repo that they can copy back.
How do I undo git rm --cached before committing?
Run git restore --staged path/to/file (or git reset path/to/file on older Git). That puts the file back in the index exactly as it was in the last commit, so it is tracked again and nothing is lost.

Sources