git rm & git mv — Managing Files
Once files are tracked by Git, simply deleting or renaming them in your file system does not fully update the repository. Git provides git rm and git mv to handle these operations cleanly.
Removing Files with git rm
The git rm command removes a file from both the working directory and the staging area in one step:
git rm old-notes.txt
After this, git status shows the deletion staged and ready to commit.
git rm vs Just Deleting
If you delete a file with rm (or your file manager), Git notices it's gone but does not stage the deletion:
rm old-notes.txt # file is gone, but Git sees it as an unstaged change
git add old-notes.txt # now you have to stage the deletion manually
Using git rm combines both steps. It deletes the file and stages the removal.
Untracking Without Deleting
Sometimes you want Git to stop tracking a file but keep it on disk — for example, a config file you accidentally committed:
git rm --cached secrets.env
The file stays in your working directory, but Git stages its removal from the repository. After committing, the file is untracked. Pair this with a .gitignore entry to prevent re-adding it.
Moving and Renaming with git mv
The git mv command renames or moves a file and stages the change:
git mv old-name.txt new-name.txt
git mv utils.js lib/utils.js
Under the hood, git mv is equivalent to:
mv old-name.txt new-name.txt
git rm old-name.txt
git add new-name.txt
Git detects the rename automatically — you will see it labeled as "renamed" in git status.
Try it!
- Create two files and commit them:
echo "alpha" > alpha.txt && echo "beta" > beta.txtgit add alpha.txt beta.txt && git commit -m "Add alpha and beta"
- Remove one with
git rm:git rm alpha.txt- Run
git status— the deletion is already staged
- Rename the other with
git mv:git mv beta.txt gamma.txt- Run
git status— it shows "renamed: beta.txt -> gamma.txt"
- Commit the result:
git commit -m "Remove alpha, rename beta to gamma"
Common Mistake
Forgetting --cached when untracking files — Running git rm secrets.env without --cached deletes the file from disk. If you only want to untrack it, always include --cached.