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?
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?
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.
git reset --hard
will undo any local modifications in the working tree, including those that were not part of the troublesome commit(s). If in doubt, it's certainly advisable to take a backup of that working tree first (a quick tar
will do nicely). –
Kenny git push origin -f localBranch:remoteBranch
because I was literally typing main:remote
, but it didn't work, you have to write git push origin -f main:main
so as to overwrite the main branch on github with my main branch. Sounds obvious now but it took me a while to figure this out. –
Brilliant I think you can rollback locally and push the result:
$ git reset HEAD^ --hard
$ git push REMOTE -f
Where 'REMOTE' is the remote name.
git revert
. On the other hand, there could be a completely different motive (as in David M. Syzdek's answer). In some cases there are no good solutions, only the least-bad one (which varies per situation). –
Conspire First of all type this command.
git log -n 4
This command will display your last 4 commits with their SHA. After this type the following command.
git rebase -i SHA_ID
Instead of SHA_ID type the SHA of the commit prior to the commit which you want to delete.
After this a file will open, on the top of that file you will see the SHA and the message of the commit which you want to delete. Delete this line and save and exit out of the file.
When you have done this you have to make a push. So type the following command.
git push -f origin master
After you are done with this you can see that your commit is deleted.
git push origin -f last_good_commit:yourEditingBranch
The last_good_commit will be the new Head of the branch. Then locally you do a hard reset:
git reset origin/yourEditingBranch
© 2022 - 2024 — McMap. All rights reserved.