What does git rm --cached do, and when would you use it?

Last updated 6 July 2026

  • git rm --cached removes a file from the index (stops tracking it) but keeps it on disk, unlike plain git rm which deletes it too.

  • Use it when you accidentally committed something like .env or build artifacts. Pair it with a .gitignore entry so it isn't re-added.

  • Use -r for directories. Past commits still contain the file (purge with git filter-repo).

  • https://timmousk.com/blog/git-rm-cached/
  • https://dev.to/ruqaiya_beguwala/day-2030-git-rm-cached-remove-files-from-staging-but-keep-locally-52l3
  • https://medium.com/from-the-scratch/git-commands-you-need-to-know-when-you-are-messed-up-e880fbf03819
  • https://git-scm.com/docs/git-rm
  • https://www.kernel.org/pub/software/scm/git/docs/git-rm.html
  • https://www.freecodecamp.org/news/learn-how-to-use-git-and-github-a-beginner-friendly-handbook/
  • https://www.freecodecamp.org/news/git-cheat-sheet-helpful-git-commands-with-examples/
  • https://medium.com/@ruqaiya.beguwala/day-20-30-git-rm-cached-remove-files-from-staging-but-keep-locally-d5180b0af0cc
All Git interview questions