Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo a git push? [duplicate]

Tags:

git

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?

like image 767
Jwan622 Avatar asked Jun 03 '16 04:06

Jwan622


People also ask

How do I undo my last push?

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> .

How do I delete a pushed commit?

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.


1 Answers

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
like image 119
Will Avatar answered Nov 09 '22 22:11

Will