53

I have a clone of a central repo at rev 2048. I want to remove the last 10 changesets on my local repo as if I was back in time two weeks ago. I suppose I could delete my local repo and do "hg clone -rev 2038" but that would be long (cloning the repo takes several minutes). Is there a way to just "unpull" some changesets?

Notes:

  • I'm not trying to backout the changesets. I'll eventually pull those changesets again from the central repo.
  • I'm not trying to update the working directory to an earlier version; I really want to affect the repository.
  • I don't have any outgoing changesets or pending modifications in my current repo and working directory.
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Sylvain
  • 19,099
  • 23
  • 96
  • 145
  • 4
    Is there a reason why you want to do this? I don't see the point, if you're going to pull the changesets again later on. What's wrong with updating to an earlier version? – R. Martinho Fernandes Dec 23 '10 at 16:44
  • Agree with @Martinho, I don't see the point. Why do you want to do this? There may be a better solution. – Mark Tolonen Dec 23 '10 at 16:50
  • That's embarrassing but after two weeks of vacations for the holidays, I don't remember why I needed to do that. Back then I did a clone from a local repo as suggested by @Mark Tolonen and that solved my problem. Next time I have to do that, I'll come back here to explain what I'm trying to do. I'm sure there is a better solution. – Sylvain Jan 06 '11 at 17:13
  • maybe you just wanted to to `hg up 2038` and start working from there. – StayOnTarget Dec 21 '17 at 21:04

4 Answers4

86

Use the strip command:

hg strip -r 2039

This command is provided by the StripExtension. It is distributed as part of Mercurial 2.8 and later, but you do need to enable it first by adding the following lines to your .hgrc or Mercurial.ini:

[extensions]
strip =

Before Mercurial 2.8, it was part of the MqExtension.

To prevent you from accidentally destroying history, the command will generate a backup bundle in .hg/strip-backup/ which you can hg unbundle again if desired.

Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
  • 11
    Please note that `strip` should only be used on unpublished changesets. – schlamar Jun 20 '12 at 07:35
  • Can also do: hg strip .... -r is optional and multi revs can be removed. – Queequeg Oct 19 '16 at 19:04
  • @schlamar, `strip` should not be used on published changesets on the master repository, that is true. However, there is no problem if you `strip` published changesets on your own clone even if they come from the master repo, on you next `pull` they will come back. – Sylvain Mar 07 '18 at 22:32
19

Cloning your local repo should be fast. I assume "several minutes" refers to a remote repo?

You can use hg clone <local repo> <new repo> -r <revision> to only clone up to a certain revision.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • 2
    +1; although I think Martinho's comment is the "answer" in this case (why bother "unpulling" if you want the changesets down the line anyway?), it's important to stress that cloning from the local repo is both possible and gives exactly the same results in less time. After cloning from local, update the `[paths]` in `.hg/hgrc` to point to remote again. – anton.burger Dec 23 '10 at 23:18
  • 1
    Cloning from the local repo is indeed much faster. Thanks – Sylvain Jan 06 '11 at 16:53
12

To remove a changeset that was already committed and pushed use :

 hg backout -r (changeset number)

To remove a changeset that was committed but not pushed use :

  hg strip -r (changeset number)
Shweta
  • 924
  • 14
  • 23
7

For versions previous to Mercurial 2.8, the Strip was part of the MqExtension.
In case you need to Enable the old MQ Extensions, you can do it by adding this:

[extensions]

hgext.mq =

to your ~/.hgrc (or mercurial.ini) file.

The Strip information used to be here but now it can be found here.

Ymagine First
  • 1,154
  • 9
  • 6