7

I pushed up some files up to github using git push <branch_name>. When I open a PR, the files show up in the changes. How do I undo this git push? In short, I tried to do a git reset of that file before pushing but apparently I mistyped the filename. Now what do I do?

How do I undo pushes to Github?

Jwan622
  • 9,962
  • 16
  • 68
  • 147
  • Is this repository contributed to by other developers? Has anyone made any changes ot it since? – Will Jun 03 '16 at 05:00

1 Answers1

19

First, make a backup, in case something goes wrong. Clone your repository as <repository>-backup with -b <branchname> and don't touch that one.

Second, find the commit hash of the last good commit in git log.

Then run:

git push --force origin <last good commit hash>:<branch>

Note that rewriting history is generally considered a bad idea, unless you're the only user of the repository. If other people have pulled down the repository with a commit change, and you force push and remove that commit, the repository will enter a corrupted state.

Unless you pushed some sensitive information you really need to remove, just revert the commit with:

git revert <commit hash to revert>
git push
Will
  • 22,773
  • 13
  • 90
  • 102