Question

I've accidentally pushed a file that I don't have rights to to a publicly accessible repo.

What can I do to selectively nuke that single file from the repo completely, so that its data is completely gone?

Was it helpful?

Solution

Git Filter-Branch is your friend in this case, well documented by the lovely Github

OTHER TIPS

Assuming the commit where you added the file is the most recent one:

git reset --hard HEAD^
git push --force

If you have more commits in the repository, you can use the interactive rebase to obliterate that commit. Use git rebase -i xxx with xxx being a commit before the one you want to nuke. Then in the editor, delete the line containing the "bad" commit. As soon as you save and exit the editor, that commit will be gone forever. If you cannot delete the whole commit, you can replace pick with edit in the file and modify the commit (e.g. remove the file from the staging area) and then run git rebase --continue

However, everyone who pulled from your repo will need to perform the same rebase manually. But then that file is already public anyway and you shouldn't rewrite history to undo your mistake.

Instead of all the rebasing you can also use git-filter-branch to remove the file, a described in the GitHub help:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch NAME_OF_THE_FILE' --prune-empty -- --all

Of course you need to git push --force after this operation, too, since it also rewrites history (with all the caveats if someone pulled from that repository and started working on it). If your repo is on github, also don't forget to contact them to clear the cache of your repository.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top