I need to remove a changeset say 123b which is somewhere in the middle of several changesets in a branch. I have commited many changesets after 123b. How do I remove the changeset 123b whithout losing recent changes
2 Answers
Technically, you can't: A changeset includes all of its parent history, so by removing 123b
, you destroy all its descendants. Any immediate children are now damaged as they refer to the nonexistent 123b
, and any children of those children are indirectly damaged, and so on.
However, there is a bright side. You can replace all of the descendants with new, improved descendants. To do that, use hg histedit
. This is a bundled extension that you must enable first. (See, e.g., What are the best and must-have hg / mercurial extensions?) (Alternatively, it's possible to use rebase or graft, but I don't recommend that—histedit is simpler.)
Note that histedit is essentially equivalent to stripping the changeset and all its descendants and then adding new changesets. As such, if you've made the changeset public by sending it to another clone, it will come right back from that other clone later, along with all of its descendants. By default, histedit won't let you edit public changesets, so that you don't make this mistake. See also https://book.mercurial-scm.org/read/changing-history.html.
(There is a better (my opinion) way, but it involves adding a non-bundled extension, specifically the evolve extension. See this answer to a related question. I have not actually used evolve, I just like the theory behind it.)

- 448,244
- 59
- 642
- 775
-
In histedit (editor window) find the changeset you want to remove and change its state from pick to drop. Exit the editor and mercurial/histedit will remove the changeset and merge children. You may be asked to resolve merge conflicts. For this reason, I recommend backing up the repo first. – Craig Jan 06 '19 at 16:37
-
As for hg-evolve, I am currently using it in my workflow/environment. I have been very impressed with it. I consider it a next step in distributed version control. Now that said, I will caution anyone from just installing hg-evolution before learning/understanding what is does. You can find a great amount of documentation here: https://www.mercurial-scm.org/doc/evolution/. While hg-evolve seems very stable, it is still very experimental and a work in progress. However, as said, I use it in my environment and am very happy with it. – Craig Jan 06 '19 at 16:46
Say you have changesets:
A-B-C-D-....
(Where your 123b could be represented by, say B here).
If B, C and none of their descendants have been pushed yet (so they only exist in your local repository) then you could first rebase C to A, and then strip B.

- 11,743
- 10
- 52
- 81