The git rm
command is used to delete files from your codebase. It removes tracked files from both the index (staging area) and the working directory, effectively deleting them from the repository.
Initial Example
$ git rm <your_file_name>
RM Options
Option | Description |
---|---|
<pathspec>…​ | Files to remove. Can include directory names to remove all files in the directory and sub-directories (requires -r ). |
-f , --force | Override the up-to-date check. |
-n , --dry-run | Show what would be removed without actually removing any files. |
-r | Allow recursive removal of directories and their contents. |
-- | Separate command-line options from the list of files. Useful when filenames might be mistaken for options. |
--cached | Remove paths only from the index, leaving working directory files intact. |
--ignore-unmatch | Exit with a zero status even if no files matched. |
--sparse | Allow updating index entries outside of the sparse-checkout cone. |
-q , --quiet | Suppress feedback messages. Normally, git rm outputs one line for each file removed. |
--pathspec-from-file=<file> | Read pathspec from a file instead of command-line arguments. If <file> is - , read from standard input. |
--pathspec-file-nul | Use with --pathspec-from-file . Pathspec elements are separated with NUL character and taken literally. |
Examples
1. Delete a File
Removes the specified file from the index and the working directory.
$ git rm example.txt
2. Force Delete a File
Forcibly removes a file from the index and the working directory, even if it has local modifications.
$ git rm -f example.txt
3. Remove a File from Index Only
Removes a file from the index (staging area) but keeps it in the working directory.
$ git rm --cached example.txt
4. Delete Multiple Files
Removes multiple files from the index and the working directory.
$ git rm file1.txt file2.txt
5. Remove Files Matching a Pattern
Deletes files that match a specified pattern.
$ git rm '*.log'