git rm Command
The git rm
command is used to delete files.
If you manually delete a file from the working directory, running git status
will show it under the Changes not staged for commit
prompt.
The git rm
command to delete files has several forms:
- Delete the file from the staging area and the working directory:
git rm <file>
The following example deletes the tutorialpro.txt
file from the staging area and the working directory:
git rm tutorialpro.txt
If the file has been modified and already added to the staging area before deletion, you must use the force delete option -f
.
Force delete the modified tutorialpro.txt
file from the staging area and the working directory:
git rm -f tutorialpro.txt
If you want to remove the file from the staging area but still keep it in the current working directory, in other words, only remove it from the tracking list, use the --cached
option:
git rm --cached <file>
The following example removes the tutorialpro.txt
file from the staging area:
git rm --cached tutorialpro.txt
Examples
Delete the hello.php
file:
$ git rm hello.php
rm 'hello.php'
$ ls
README
Remove the file from the staging area but keep it in the working directory:
$ git rm --cached README
rm 'README'
$ ls
README
You can perform a recursive deletion, meaning if you provide a directory as an argument, it will recursively delete all subdirectories and files within that directory:
git rm -r *
Executing this statement in a specific directory will delete all files and subdirectories within that directory.