Skip to content

Removing a file from a repo

Matteo Manferdini edited this page Jul 31, 2013 · 2 revisions

Removing a file from a repo is straightforward: you just delete the file in your file system and commit your changes. Now your file is gone, or so it seems.

As a version control system, Git keeps track of all the changes to the repo. This means that, although your file has been removed from the current working copy (and future ones), that file is still in the history of the repo. This is good, because you might want to go back in the history and recover deleted files that become useful again.

There are cases though when you want a file to be completely deleted from a repo. For example, if you commit a big file (like a video), that file will keep taking space and anybody cloning the repo will have to download it, although the file has apparently been deleted. Many files like this can make a repo really big and hard to download. Other times you might accidentally submit a file with sensitive data that you want to remove from the whole history of the repo.

To do so, you do a recursive delete of the file. This does not only delete the file from the current working copy, but also from the whole history.

Some visual Git tools might have the option in their interface, so look for it. If this functionality is not provided, it's still possible to do it from the command line.

Open your terminal and navigate to the folder of the repo. Then type:

git filter-branch --index-filter 'git rm --cached <file>' HEAD

Where <file> is the name of the file you want to delete. This will remove the file from the repo, including all the branches.

Clone this wiki locally