How to remove a commit that has been pushed to the remote repository using Git

shadowfax picture shadowfax · Mar 16, 2012 · Viewed 16.5k times · Source

I've pushed a couple of commits to the remote repository and found they are creating problems.

How can I go back to the previous version? That is, removing the two latest commits?

Answer

David M. Syzdek picture David M. Syzdek · Mar 16, 2012

Since you have already pushed the commits to a remote repository, the best way is probably to revert the two commits so that you do not create any problems for anyone who has already pulled from the remote repository.

Examples use the following commit history:

e512d38 Adding taunts to management.
bd89039 Adding kill switch in case I'm fired.
da8af4d Adding performance optimizations to master loop.
db0c012 Fixing bug in the doohickey

If you just want to revert the commits without modifying the history, you can do the following:

git revert e512d38
git revert bd89039

Alternatively, if you don’t want others to see that you added the kill switch and then removed it, you can roll back the repository using the following (however, this will cause problems for others who have already pulled your changes from the remote):

git reset --hard da8af4d
git push origin -f localBranch:remoteBranch

where localBranch is the name of the local branch and remoteBranch is the name of the remote branch.