← Back to Git Course | Chapter 2: Basic Commands | Lesson 14 of 18

Git rm Command

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

bash
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

bash
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

bash
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

bash
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

bash
git rm -r build/

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.