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.txtfrom 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
--cachedoption:git rm --cached file.txtThis will unstage
file.txtbut keep it in the working directory.Remove a Directory Recursively
To remove a directory and all its contents, use the
-roption: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.txtfrom thesrcdirectory and stages the removal.Unstage a File but Keep It Locally
git rm --cached README.mdThis command unstages
README.mdbut keeps it in the working directory.Remove a Directory and Its Contents
git rm -r docs/old_directoryThis command removes
old_directoryand all its contents from thedocsdirectory and stages the removal.
Using git rm helps manage file removals in your Git repository efficiently.