Git rm

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.



Tutorials dojo strip

Initial Example

$ git rm <your_file_name>




RM Options

OptionDescription
<pathspec>…​Files to remove. Can include directory names to remove all files in the directory and sub-directories (requires -r).
-f, --forceOverride the up-to-date check.
-n, --dry-runShow what would be removed without actually removing any files.
-rAllow recursive removal of directories and their contents.
--Separate command-line options from the list of files. Useful when filenames might be mistaken for options.
--cachedRemove paths only from the index, leaving working directory files intact.
--ignore-unmatchExit with a zero status even if no files matched.
--sparseAllow updating index entries outside of the sparse-checkout cone.
-q, --quietSuppress 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-nulUse 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'
Scroll to Top