What is git rm?

Answer

git rm removes files from both the working directory and the Git staging area (index), staging the deletion for the next commit. It is different from simply deleting a file with rm — if you delete a file manually, you still need to git add the deletion; git rm does both in one step. Usage: git rm file.txt — delete from working directory and stage the deletion; git rm -r dir/ — recursively remove a directory; git rm --cached file.txt — CRITICAL: removes the file from tracking (index) WITHOUT deleting from disk. Used when you accidentally committed a file that should be in .gitignore (secrets, node_modules). After git rm --cached, add the file to .gitignore and commit the removal; git rm -f file.txt — force removal even if file has uncommitted changes; git rm "*.log" — remove files matching a pattern. After git rm, the deletion is staged — commit to finalize: git commit -m "Remove obsolete file". Common workflow: accidentally committed .env file → git rm --cached .env → add to .gitignore → commit → rotate any exposed secrets.