More Git answers

by Danny
Published 2 min read

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.

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.


_10
git rm --cached .env
_10
echo ".env" >> .gitignore
_10
git add .gitignore
_10
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


_10
git rm --cached config/local.json
_10
echo "config/local.json" >> .gitignore
_10
git add .gitignore
_10
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.


_10
git rm -r --cached node_modules
_10
echo "node_modules/" >> .gitignore
_10
git add .gitignore
_10
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.


_10
git rm -r --cached .
_10
git add .
_10
git status # check the list of deleted files before committing
_10
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.


_10
git check-ignore -v .env
_10
# .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.
#

Was this resource helpful?