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?
If you want to revert the last commit just do git revert <unwanted commit hash> ; then you can push this new commit, which undid your previous commit. To fix the detached head do git checkout <current branch> .
To delete commits from remote, you can use the git reset command if your commits are consecutive from the top or an interactive rebase otherwise. After you delete the commits locally, push those changes to the remote using the git push command with the force option.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With