How to Delete a File from a Git Repository 💣

There are many ways to remove a file from a Git repository. Some of them require multiple steps if you want to keep the file locally. Fortunately, one of them is simpler than that and worth remembering!

1 min read
How to Delete a File from a Git Repository 💣
Photo by cottonbro on Pexels.

If you need to remove a file that has already been committed to the Git tree, you can use the git rm command. It will remove the specified file from the index AND from the disk.

git rm FILE_PATH

But what if you still want to keep the file locally? For example, it might be a file that was accidentally committed and that you still need. 🤔

Fortunately, Git provides an argument you can add to remove the file from the index only:

git rm FILE_PATH --cached

With this simple argument, you'll be able to keep the file in your project while deleting it from Git! 😉