Vishal
Vishal

Reputation: 45

How to remove a changeset from hg repo without loosing changesets commited after that

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

Upvotes: 0

Views: 660

Answers (2)

StayOnTarget
StayOnTarget

Reputation: 13037

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.

Upvotes: 1

torek
torek

Reputation: 489123

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.)

Upvotes: 1

Related Questions