PUBLISHED ON: JULY 12, 2021
How to Delete Files on Git
After a period of time, we may end up with a lot of unwanted files in our Git repository. These files do not add any value to our project and just clutter our repository. We can remove these files from our repository by using the Git Rm command. The Git Rm command is similar to the Linux command rm that stands for remove. Let's learn how to use this command to delete unwanted files and tidy up our repository.
Deleting Files
We can delete a file manually by searching for it in the repository and then deleting it but we will have to stage this deletion. The Git Rm command will do this in a single step. It is also capable of removing the file from Git Repository but still storing it in the local file system. Let's learn how to use the Git Rm command.
Deleting a Single File Using Git Rm
We can simply mention the file name that we want to delete and the Git Rm command will remove it from our repository and the local file system.
$ git rm <file-name>
Deleting Multiple Files Using Git Rm
We can also delete multiple files at once by separating the file names with whitespace.
$ git rm <file-1> <file-2> <file-3>
Use the -r flag, if you wish to recursively delete all the files present in a subdirectory.
$ git rm -r <sub-directory>
Another thing that can be used to delete multiple files at once is a Glob Pattern. We can use it to delete multiple files that follow a similar naming convention. For example to delete all text files we can use *.txt as all text files will have a .txt extension.
$ git rm <glob-pattern>
Deleting Files With Unsaved Changes
Git may block a deletion if the file that we are trying to delete has local modifications or unsaved changes. This is a safety feature that prevents us from removing changes that we may not be able to restore. To bypass this safety use the -f or the --force option with the Git Rm command. The following image shows how the output when Git blocks a removal.
$ git rm -f <file-name>
Deleting Files from just the Git Repository
Git Rm will remove the files from the Git repository as well as the file system. But we can just remove the file from the repository by using the --cached command. This will remove the file from the staging area but the file will continue to exist in our system.
$ git rm --cached <file-name>
Committing After Deleting
The Git Rm command will delete a file but we also need to commit this deletion to make sure that our repository reflects this change. This deletion is automatically staged by Git and we see this by using the Git Status command. The following image shows the output of the Git Status command after a file was deleted.
$ git rm unwantedFile.txt
$ git commit -m "Remove an unwanted file"
Summary
We will often find unwanted files in our repository. Git provides us the Git Rm command that we can use to remove unwanted files. By default, the Git Rm command will remove the file from the repository as well as our file system but we can use the --cached command to just remove the file from the repository and not from the file system. Git also has a safety mechanism that will block a deletion if some unsaved changes are present in the files. We can use the -f flag to forcefully remove such a file. Remember to commit once you delete a file so that the is aware of the deletion.