During the development process bunch of files will be added to your repository. On the other hand, a bunch of them will be removed. Either because they are no longer needed or because they became surplus to the requirements. Deletion of something is easy in the IT industry, it tends to happen accidentally and when we least want it to happen, right? The same is with file deletion on Git. But to avoid all the confusion between deletion of the file from the repository or from the filesystem, in this tutorial we will learn how to delete files on Git. That way we can be sure that accidental removal of the files will become thing of the past.
How to Delete a File on Git – Repository and Filesystem
Git command for file deletion is pretty much similar to the command that is being used on all Unix distributions. Each time you delete a file on git, you have to commit and push your changes. We’ll show you how to do that in the example below.
First and foremost, let’s remove the file.
git rm test_file
The output will look like the one below.
Outputrm ‘test_file’
What this did is that it removed the file from the repository and the filesystem. The next step is to commit this change. Here’s how you can do that.
git commit -m “Removed test_file from Git repository and filesystem”
And the last step is to push the change to your remote repository. Simple Git push will take care of that.
git push
Congratulations! The file called “test_file” has successfully been removed. I just want to point out that the file that we used was just an example. The same goes for the commit line in quotation marks. Both filename and that line will have to be the actual file that you want to remove and commit line which fits the action that you just performed.
How to Delete File on Git – Recursively
This one is identical to the one that would be used on Unix-based distributions as well. Same command, same philosophy, with the addition of the “-r” flag.
Action like this one is extremely useful when you need to remove the whole directory, or even several directories at once. The example below shows how to recursively remove the directory.
git rm -r dir_for_testing
Again, we have to commit the change.
git commit -m “Removed dir_for_testing from Git repository”
And finally, push it.
git push
What we did with these three commands is that we actually removed the directory called “dir_for_testing” along with all the content of it, and that includes subdirectories.
NOTE: Be extremely careful when you are removing something recursively! This can cause damage beyond repair in your repository. Always double-check your work and syntax which is being used.
How to Delete File on Git – Just Repository
If you are facing a scenario in which you want to delete from your Git repository, but you want to keep it in the filesystem, you’ll have to use the “–cached” flag. So same syntax, same process, just add the flag.
git -rm --cached test_file
git commit -m “Removed test_file from Repository”
git push
Conclusion
We hope that you learned something new today and that you will put it to good use. I’ll point out one more that it is crucial to always check your syntax. You don’t want to remove something by accident.