Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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
@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
@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
@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

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

share|improve this answer
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
up vote 1 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
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. – Hooper May 2 at 16:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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