delete file from git repository

How to Delete a File in Git Repository

Git is a popular, distributed version control system used by many software development teams around the world. Git keeps track of all user actions such as file addition, modification as deletion. While working with git, you may need to delete one or more files, due to some reason or the other. In this article, we will learn how to delete file in Git repository.


How to Delete a File in Git Repository

Many of git commands are similar to those of Linux, making them easy to learn. Here is the command to delete file in git repository. Please note, if you see ‘permission denied’ error for any of the following commands, add sudo keyword at the beginning of each of the following commands.

$ git rm file_path

For example, if you want to delete a file data.txt from your git repository, then navigate to your git repository first and then execute the following command.

$ git rm data.txt

Please remember, if you do not execute the above command from within git repository, then you will get an error saying ‘file not found’.

Once the above command is successfully executed, the above file will be removed from both the git repo as well as your filesystem. If you use only rm command, instead of git rm command, then the file will be removed from filesystem but not git repository.

Next, you need to commit the changes to your git repository.

$ git commit -am "deleted data.txt"

Finally, push the commits to master. Instead of using origin branch, you can also make changes & push them from a different branch.

$ git push origin master


Delete Folder from Git Repository

Sometimes you may need to delete one or more files recursively from git repository. In such cases, use the -r option to do it. For example, if you want to delete /projects folder from git repository, then run the following command.

$ git rm -r /projects

Once you have deleted the required files, commit changes.

$ git commit -am "deleted projects folder"

Push changes to master with the following command.

$ git push origin master


Delete Files from Repository Only

In the above examples, git will delete file from your filesystem as well as git repository. But sometimes, you may want to keep the file in your filesystem but not in repository. In such cases, use rm –cached command. The –cached option will remove file from the repository. Here is an example to remove file data.txt only from the repository but not your disk.

$ git rm --cache data.txt
$ git commit -m "removed data.txt from repository only"
$ git push

In this article, we have learnt different ways to delete files from git repository. You can modify the above commands as per your requirements. Remember to navigate to your git repository’s folder before executing any of the above commands.

Also read:

What does __file__ mean in Python
Sed Command to Replace String in File
What is __name__ in Python
How to Find Non-ASCII Characters in MySQL
How to Use Reserved Words as Column Name in MySQL

Leave a Reply

Your email address will not be published. Required fields are marked *