rm
git rm
is a command used to remove files from the working directory and the staging area in a Git repository. It stages the removal of the specified files for the next commit.
Basic Usage
Remove a File
To remove a file from the working directory and stage the removal, use:
git rm file.txtThis will delete
file.txt
from the working directory and stage the removal.Remove a File from the Staging Area Only
To remove a file from the staging area but keep it in the working directory, use the
--cached
option:git rm --cached file.txtThis will unstage
file.txt
but keep it in the working directory.Remove a Directory Recursively
To remove a directory and all its contents, use the
-r
option:git rm -r directory/This will delete the directory and all its contents from the working directory and stage the removal.
Examples
Remove a Single File
git rm src/old_file.txtThis command removes
old_file.txt
from thesrc
directory and stages the removal.Unstage a File but Keep It Locally
git rm --cached README.mdThis command unstages
README.md
but keeps it in the working directory.Remove a Directory and Its Contents
git rm -r docs/old_directoryThis command removes
old_directory
and all its contents from thedocs
directory and stages the removal.
Using git rm
helps manage file removals in your Git repository efficiently.