Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I completely nuke a file from a git repo?

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?

like image 710
Charles Randall Avatar asked May 14 '12 14:05

Charles Randall


People also ask

How do I delete a file from GitHub repository?

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.


2 Answers

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

like image 97
codatory Avatar answered Sep 30 '22 07:09

codatory


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.

like image 43
ThiefMaster Avatar answered Sep 30 '22 07:09

ThiefMaster