When you work with Git version controlling system, you may want some files to be removed from the Git indexing and working directory.
Git rm command is used to serve the purpose. rm stands for remove, and as it sounds, it will perform the operation of removing one or more tracked files from the Git staging index and the working tree.
There is no such possible option in Git to remove a file from the working directory and still keep it in the index; for that purpose, you need to use /bin/rm.
Keep in mind that the file you are specifying for removal operation is identical to the tip of the branch, and also, there are no updates to their contents expecting to be staged in the index. However, this problem can still be solved with the -f or –force parameter with the command.
Table of Contents
Important Options to be Used with Git rm Command.
1. < filename >
One of the most common parameter options to provide with the command is simply the path or name of one or more files to remove them. If you are already in the directory, you can give the filename, or else you have to provide the file path.
It’s possible to delete multiple files by separating the filenames with spaces or using a wildcard pattern, i.e., myfolder/* will remove all the files in the myfolder directory.
git rm myfile.txt
This option allows the command to delete everything inside the specified folder recursively. All of the content of the specified parent directory, be it files or more folders, will be removed.
git rm folder1 -r
git rm myfile.txt --cached
git rm folder1/* --dry-run
git rm folder1/* --quiet
Git workflow is quite handy, and we all have to use it in our development career sooner or later. Removing files from being tracked by the version controlling system like Git is as essential as adding them. In this article, we demonstrated to you the git rm command’s working mechanism along with different vital options to work with this command.