Github push to wrong branch


I accidentally pushed a commit to the wrong branch and I looked into revert, used it and now I am inside the vi editor with “changes to be committed” from another developer. I do not want to ruin his work by doing this. I don’t know whether to kill my console or what.

Nathan Hains

If I do a revert, the editor for the commit message contains a comment that includes the hint “an empty message aborts the commit”. So: Delete all non-comment lines, save, and exit the editor. You might have to reset/checkout files that would have been modified by the revert after.

For the future, git revert doesn’t do anything related to pushing, as described in its documentation:

Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one).

If anything goes wrong, you can always not push the “revert” commit and git reset your local branch to the commit before to discard the revert commit. :slightly_smiling_face:

Undoing the effects of a push to the wrong remote branch is tricky. By default git push will not delete any existing commits on the remote branch. If you know exactly at which commit the remote branch should be in theory you could force-push it, but in practice that would cause more trouble if someone has fetched or even pushed since. :warning:

Your message implies you are working with at least one other developer, so you should probably discuss with your team how to handle the situation.