5

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Charles Randall
  • 6,920
  • 12
  • 33
  • 38
  • 4
    http://help.github.com/remove-sensitive-data/ – Artefact2 May 14 '12 at 14:43
  • possible duplicate of [How do I remove sensitive files from git's history](http://stackoverflow.com/questions/872565/how-do-i-remove-sensitive-files-from-gits-history) – CharlesB May 14 '12 at 14:48
  • 1
    Please, give more information, Do you want to completely delete all information of this file from your history or just HEAD? Did the file filter into one commit or more? Do you want to keep the commit and delete just a file or the delete the whole commit?, is it in multiple branches? – KurzedMetal May 14 '12 at 14:51
  • 1
    possible duplicate of [Completely remove file from all Git repository commit history](http://stackoverflow.com/questions/307828/completely-remove-file-from-all-git-repository-commit-history) – Francisco Puga Aug 22 '15 at 08:54

2 Answers2

6

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

codatory
  • 686
  • 4
  • 5
2

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.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636