I committed and pushed some changes to the wrong branch. I found a way to fix it, but it seems overly complicated. Is this really the easiest way?

  1. on the branch that you made your changes, type hg parent
  2. run hg diff -r <parent> -r <with_changes> > patch.diff
  3. hg up <proper_branch>
  4. hg import --no-commit patch.diff
  5. hg ci -m 'committing changes to proper branch'
  6. hg up <bad_branch>
  7. hg parent to find previous changeset on this branch that is good
  8. hg ci --close-branch -m 'closing bad branch'
  9. hg up <last_good_changeset>
  10. add a space or other minor change
  11. hg ci -m 'making tip'
  12. hg push

Something to that effect anyway. I did it in a slightly different order; I made the good changeset the tip again, and had to do a force commit because it creates a new head, then had to run

hg heads `hg branch`

To find the bad branch again, then close it. Now it still shows the bad changeset as the "tip" because I closed it after I "re-tipped" the good branch, but when updating to that branch, I guess it takes the most recent non-closed head?

Seems like quite a ridiculous process to fix such a simple mistake.

Is there any easier way to do this? Why not?

  • 1
    I'm not an expert in mercurial, but my thinking is that you should leave it complicated. If it's painful to undo, maybe it'll deter you from doing it again :) – Joel Etherton Feb 21 '13 at 20:41
  • 3
    @JoelEtherton You can't deter accidents. – Joe Z. Feb 21 '13 at 21:02
  • @Joe Zeng: Accidents happen when people are inattentive. You can deter inattentiveness. Ergo, you can deter accidents. – Joel Etherton Feb 21 '13 at 21:04
  • 1
    @JoelEtherton Accidents also happen when people are attentive. Making the consequences hard to fix is not a good deterrent from doing something when you don't even know you're doing it. You can never be attentive enough that accidents never happen. – Joe Z. Feb 21 '13 at 21:11
  • 2
    @JoelEtherton I've seen enough people use that smiley face to mask a real criticism that I can't tell. Sorry for misunderstanding you. – Joe Z. Feb 21 '13 at 21:24

I did find a cleaner solution. You don't need to "touch" the branch to bring it back to tip, nor close off the bad branch.

Instead, just up to the bad branch, hg revert -r <last_good_commit> -a then recommit.

Since you're upping to the tip of the bad branch it doesn't create a new head, and then your reverted changes get pushed to the tip instead.


Is there any easier way to do this?

Yes. hg help rebase: rebase changeset to the new correct parent and push

PS: you have to enable Rebase extension before using command

  • The issue with this is it assumes that the two branches are identical aside from the changes you just made. Usually there's a few other changes I need to keep, so really, I need a diff of the changes I made so that it can be applied to the proper branch. – mpen Mar 14 '13 at 19:07

hg rollback will rollback the LAST transaction if you have not pushed. If you've committed or pushed work after the transaction you want to make go away, it will not work. hg help rollback for details.

  • First sentence...I said it's already pushed ;) – mpen Mar 14 '13 at 19:05
  • this is not a good idea. You will lose your work using this. – Jared Hooper May 2 '16 at 16:34
  • Sorry I missed the push note. You will not lose any work, you will rollback your work directory to what it looked like before the commit. You can then make any changes you want and commit again. – Mark Aug 22 '16 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.