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

Git rm Command

git rm is like throwing away a paper from your folder and also telling the librarian to cross it off the list, all in one go.
Syntax
bash
git rm file_name
git rm --cached file_name
git rm -f file_name

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/
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Deleting a file with your file manager and thinking it is removed from Git, when you still need git rm or git add -A to record the deletion.
  2. Running git rm file when you wanted to keep the file, when git rm --cached file untracks it and leaves it on disk.
  3. Using git rm on a file with uncommitted changes and being blocked, then using -f and losing that work.

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.