Completely remove file from local and remote (GitHub) repository

This is the short version of “Remove sensitive data” article in GitHub Help. And an alternative solution to the one presented [here](Remove file from repository and from all revisions.txt). It assumes, that you are the only collaborator to repository, so you can perform steps at once (you don’t have to contact others and wait until they rebase your changes) and that your repository doesn’t use tags, so you don’t have to perform all steps.

If any these two assumptions is not correct in your case, then avoid this article and rather follow to mentioned article. Keep in mind, however, that in some certain situations, you have to contact GitHub Support in order to finish entire procedure.

I wanted to remove sensitive.file from one of my repositories, in a brute-force way, that is, to remove it from current repository state and to get rid of all traces of this file in all commits, revisions and entire git history. Note, that this means rewriting your repository history, so proceed with cautions, if you’re doing this for the first time.

Steps included:

1. Emptying repository out of sensitive.file file:

git filter-branch --force --index-filter
"git rm --cached --ignore-unmatch sensitive.file"
--prune-empty --tag-name-filter cat -- --all

2. Forcing changes to GitHub:

git push origin --force --all

3. Purging locally cached changes:

git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now

After that, git reported me, that there is nothing to commit (git status) and that my local repository is up-to-date with remote one (git pull + git push).

My surprise was quite huge, when I realized, that I still can access that file, using URL, I had open in one of tabs, that was leading to one of its early copies in GitHub. Refreshing page didn’t bring any change. File was clearly there. I contacted GitHub with this problem, and got immediate response, that they had to run git garbage collector (git gc) on their side, to finish this process.

Finally, after that, mentioned link started to show 404 page.

You may be also interested in reading this: “Remove file from repository and from all revisions“.

Leave a Reply