git rm command

The git rm command is used to remove files or directories from your working directory and the index (staging area) in Git.
This means that the files will be deleted from our file system and also marked for removal in the next commit. Here’s how we can use the git rm command along with its various options: Basic Usage To remove a file and stage the removal for the next commit: Syntax git rm <file-name> | <directory>
It will remove the given file, or all the files from the given directory and recursively all sub-directories, but this requires the -r option to be explicitly given.
Example: To remove a file example.txt, we can use the following command.

Bash

git rm example.txt

If we want to remove a directory named folder1 and its contents from the Git repository, you can use the following command:

Bash

git rm -r folder1/

Removing Files from the Index Only If we want to remove a file from the index (staging area) but keep it in the working directory (i.e., the file will not be deleted from our file system), use the --cached option:

Bash

git rm --cached <file>

Example:

Bash

git rm --cached example.txt