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?
To remove a commit you already pushed to your origin or to another remote repository you have to first delete it locally like in the previous step and then push your changes to the remote. Notice the + sign before the name of the branch you are pushing, this tells git to force the push.
To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.
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.
I think you can rollback locally and push the result:
$ git reset HEAD^ --hard
$ git push REMOTE -f
Where 'REMOTE' is the remote name.
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