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. The output will look like the one below. 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. And the last step is to push the change to your remote repository. Simple Git push will take care of that. 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. Again, we have to commit the change. And finally, push it. 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.
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.