Git rm Command
In this page:
Removing Files from Directory and Git
git rm <file> deletes a file from both your working directory and your Git tracking in one step -- it removes the file from disk and stages that deletion, so your next commit will record the file as gone.
Example: Removing Files from Directory and Git
git rm old-file.txt
Removing from Staging Area Only
If you want Git to stop tracking a file but keep the actual file on your disk -- common for a config file that shouldn't be shared -- git rm --cached <file> untracks it without deleting it locally.
Example: Removing from Staging Area Only
git rm --cached config.local.json
Forcing File Removal
Git blocks git rm on a file that has unsaved staged or working changes, as a safeguard against losing work you haven't committed yet; the -f (force) flag overrides that safeguard when you're certain you want to proceed anyway.
Example: Forcing File Removal
git rm -f locked-file.txt
Dry-Running File Removal
git rm -n <file> performs a dry run, listing exactly which files the command would remove without actually touching anything -- worth running before any destructive rm command involving wildcards or whole folders.
Example: Dry-Running File Removal
git rm -n *.log
Mass File Deletion
Combining git rm with a wildcard or folder path, like git rm -r build/, removes and stages the deletion of an entire group of files at once, which is much faster than deleting them individually.
Example: Mass File Deletion
git rm -r build/
Chapter Quiz — Complete all 18 topics to unlock
0/18 topics done
Complete these topics first:
- Git init Command
- Git New Files
- Git Staging Environment
- Git clone Command
- Git status Command
- Git help Command
- Git add Command
- Git commit Command
- Git tags Command
- Git stash Command
- Git log Command
- Git diff Command
- Git show Command
- Git rm Command
- Git mv Command
- Git restore Command
- Git clean Command
- Git ignore (.gitignore)