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?
Browse to the directory in your repository that you want to delete. In the top-right corner, click , then click Delete directory. Review the files you will delete. At the bottom of the page, type a short, meaningful commit message that describes the change you made to the file.
Git Filter-Branch is your friend in this case, well documented by the lovely Github
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With