Completely undo errant Github push

For any Git or Github repo, you can undo (delete from git history) the last or previous commits to eliminate huge/numerous/wrong files pushed. If you only git revert, this leaves the big mess inside the .git directory, slowing down operations and wasting space.

  1. copy your screwed up directory to a backed up location e.g. Dropbox.
  2. assuming it’s the last commit and push you want to delete from the Git history

     git reset --hard HEAD^
     git push -f
  3. If you need to copy any files from the old repo, do so now and make a new commit/push in the new repo.
  4. Every other computer with a copy of this repo will need to reclone the repo, since if they git pull it will put all the old files back in.

Implicit in this procedure is that those with write access to your Github repo can overwrite history, potentially causing permanent file loss. Remember that Git is a revision tracking system, NOT a backup system. You can use Dropbox as a Git backup, for example.

Go to your Github repo (assuming you’re the repo owner), click Settings, Branches and Protect your master branch and other important branches. Then for each Protected branch, click Edit, Protect this branch and pick the appropriate level of protection.