Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you "undo" a local commit to the wrong branch

I did some code changes in Visual Studio from the main branch and tried to commit the changes in a new branch in TortoiseHG, but apparently I accidentally committed the corrections to the main branch. I have not pushed the changes to the remote repository but if I try to do a "Backout" on the change I get the error message "Cannot backout change on a different branch", even though it clearly states that the changes have been committed to the main branch.

Does anyone know why I get this error message and how I can move the changes into the correct branch?

like image 696
Stig Perez Avatar asked Nov 21 '25 08:11

Stig Perez


2 Answers

As long as your changes are not yet pushed anywhere else there are two options.

a) Use hg rebase to move one changeset and all of its descendents on top of another changeset:

 hg rebase -s FIRST_CHANGESET_TO_MOVE -d DESTINATION

b) The less recommended way, which only works if you want to undo ONE commit and haven't pulled or committed anything since, is to use hg rollback. But even in those cases hg rebase seems like the easier thing to use.

like image 106
planetmaker Avatar answered Nov 25 '25 00:11

planetmaker


Firstly you don't want to Backout as that is for undoing pushed / published changes.

There are multiple ways to do this fix this:

Repository -> Rollback/Undo

This will undo you last commit and leave your working directly with the uncommitted changed. You can then recommit making sure to select the new branch name.

If for some reason Rollback/Undo doesn't work:

  1. Update to the parent of the commit.
  2. Right click on the commit -> Revert all Files
  3. Commit the changes again - making sure to select the new branch name.
  4. Right click on the previous commit you want to remove -> Modify History -> Strip (The strip extension will need to be enabled in File -> Settings -> Extensions)
like image 28
Tom Avatar answered Nov 25 '25 00:11

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!