0

I use Mercurial, and I immediately push every commit to BitBucket.

I recently made a commit to my local repository, and pushed it to BitBucket via hg push. Later, I realised that I shouldn't have made that commit, so I tried:

$ hg rollback
repository tip rolled back to revision 37 (undo push-response)

But the file in Xcode did not change. I closed the Xcode window and reopened it, but no change. I tried to revert the rollback in Bitbucket, but that didn't work either:

$ hg push ssh://hg@bitbucket.org/myuser/myproject
pushing to ssh://hg@bitbucket.org/myuser/myproject
searching for changes
no changes found

How do I rollback, both locally and in BitBucket? I want the code to go back to the state it was in before the bad commit.

Kartick Vaddadi
  • 4,818
  • 6
  • 39
  • 55

1 Answers1

1

You can't rollback in Bitbucket, you have to strip the change from the repository.
In Bitbucket, go to the repository Settings and then Strip commits. Enter the hash id of the commit and click Preview strip, then if you're happy that it's going to strip the right commit you can confirm it.

Now you can remove or edit the commit in your local copy.
hg rollback isn't recommended these days, if you can fix the commit you can use hg commit --amend to update it before pushing again.

Rollback will remove the commit and leave the changed files in an uncommitted state in your working directory. It appears from the message you got that your rollback succeeded, and I wouldn't expect the file to change in XCopy because it leaves the change in your working directory, but if you look at what the tip of the repository is now your commit should be gone and if you look at the state of your working directory you should have uncommitted changes corresponding to your unwanted change.
You should be able to hg revert any uncommitted changes you don't want.

If the commit is still there, and you want to get rid of it completely and don't want to keep the code at all you can use hg strip to remove it (you may need to activate the strip extension in the mercurial settigs first).

Mercurial - Finding and fixing mistakes

Strip extension

Nanhydrin
  • 4,332
  • 2
  • 38
  • 51
  • 1) I don't want to make changes directly in the BitBucket web UI. Instead, I want to make the change locally, verify that it builds and runs, and only then push to BitBucket. 2) You wrote that "Rollback will remove the commit", but when I run hg history, the commit is still there. 3) I don't understand why the file won't change in Xcode if it's changed on disk. 4) Thanks for telling me to avoid rollback. I'll try to use strip. – Kartick Vaddadi Jan 03 '17 at 06:01
  • 1) You can't push a delete of a commit. All you could do is use `hg backout` to create another commit that reverses that changes and gets committed on top of the faulty change. But now you've got two commits. 2) Rollback should remove the commit, you might need to change the phase of the commit back to draft(`hg phase --draft --rev your_revision_number --force`) and try the rollback again. 3) If the rollback succeeds, the file won't change on disk, because although rollback removes the commit from the history it just dumps the changes into the working directory. – Nanhydrin Jan 03 '17 at 23:44