Completely undo errant git remote push

For any Git remote repo, you can undo (delete from git history) previous commits to eliminate wrong files via git push. If you only git revert, this leaves the big mess inside the .git directory, slowing down operations and wasting space.

Erase previous git commits

Before attempting recovery, copy local project to a remote backup location e.g. Dropbox.

Assuming it’s the last commit and push to delete from the Git history:

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

Any time the git --force option is used, work can be permanently and irrecoverably lost, so always use care.

Reapply desired commit work

Copy any files from the old repo and make a new commit/push in the new repo. Every other computer with a copy of this repo will need to:

  1. make a backup copy of their local repo, if they did any work that needs to be saved
  2. git clone the repo again and likewise copy their changes back in.

Yes, they could git reset, but they might erase their work in that repo, if any.

Git remote write permissions

Implicit in this procedure is that those with write access to the remote Git repo can overwrite history, potentially causing permanent file loss. Remember that Git is a revision tracking system, NOT a backup system.

Restrict Git remote write access: for GitHub, from the repo branches Settings page, Add Rules according to your needs.