95

I'm using Mercurial and I've got into a terrible mess locally, with three heads. I can't push, and I just want to delete all my local changes and commits and start again with totally clean code and a clean history.

In other words, I want to end up with (a) exactly the same code locally as exists in the tip of the remote branch and (b) no history of any local commits.

I know hg update -C overwrites any local changes. But how do I delete any local commits?

To be clear, I have no interest in preserving any of the work I've done locally. I just want the simplest way to revert back to a totally clean local checkout.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Richard
  • 62,943
  • 126
  • 334
  • 542
  • Possible duplicate of [Is there any way to delete local commits in Mercurial?](https://stackoverflow.com/questions/2338986/is-there-any-way-to-delete-local-commits-in-mercurial) – StayOnTarget Oct 24 '18 at 11:41

7 Answers7

132

When the simplest way (a new hg clone) isn't practical, I use hg strip:

% hg outgoing -l 1
% hg strip $rev # replace $rev with the revision number from outgoing

Repeat until hg outgoing stays quiet. Note that hg strip $rev obliterates $rev and all its descendants.

Note that you may have to first enable strip in your Mercurial settings.

PS: an even smarter approach is to use the revset language, and do:

% hg strip 'roots(outgoing())'
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
just somebody
  • 18,602
  • 6
  • 51
  • 60
21

You'll want to make a local clone where you preserve only the changesets that are also present in the remote repository. Use TortoiseHg, hg log or similar to figure out which of your revisions is that lastest revision you didn't make (the one before the mess started). Using hg outgoing can help here -- it will list all the changesets you made -- pick a revision number earlier than any of those.

If the target revision is called good and your clone is called foo, then do:

hg clone -r good foo foo-clean

This will be a fast, local operation -- there is no reason to download everything again. The foo-clean clone will only contain changesets up to revision good. You can now replace foo-clean/.hg/hgrc with foo/.hg/hgrc in order to preserve your repository-local settings such as the default push/pull path.

When you are satisfied that foo-clean has everything you need from foo, then simply delete foo and rename foo-clean to foo. Do a hg pull to get any new changesets from the remote repository into your clone and continue like normal.


If nobody has pushed new changesets to the remote repository, then it is very simple to determine which revision you want to use as good above: hg id default will tell you the ID of the tip in the remote repository.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
  • I'm having the same situation, but also have a lot of file in the repo directory but are in the hgignore. Can't install hg strip and would like to drop the local commits without replacing the whole clone. Any ideas? -- Ah! If I haven't added any files, I should be able to just copy the other clone into my old directory!? – Peteter Feb 15 '11 at 13:29
  • Be careful with this method if you have multiple branches, though. Cloning up to a specified revision only gets the changesets on the branch of the specified revision. You may then have to `hg pull -r` for the last "good" revision of each additional branch. – Yitz Jan 09 '13 at 14:11
9

Ok. So just delete all the local stuff, hg init the new local repository and hg pull the latest tip you have. Don't forget to hg update after this.

alemjerus
  • 8,023
  • 3
  • 32
  • 40
  • Thanks. When you say 'delete all the local stuff', do you mean delete the entire repository, or just my changes? What does hg init do (have Googled but can't see a simple explanation) and do I need to use any flags with it? Will it delete things like my Mercurial .hgrc preferences? – Richard Jan 26 '10 at 12:16
  • Update: if I delete everything in the project root directory, then type 'hg init', I get 'abort: repository . already exists!'. Do I need to set up and work from a new directory? I'd rather not if possible... surely there must a simple way to kill everything I've done locally?! – Richard Jan 26 '10 at 12:26
  • Yes, delete everything including all the repository stuff. Start from a new clean folder. Your preferences you can save in backup file, and then restore them into the new repository you create with hg init reponame. Details on init and stuff: http://hgbook.red-bean.com/read/mercurial-in-daily-use.html – alemjerus Jan 26 '10 at 19:13
  • 6
    alemjerus: `hg init` + `hg pull` = `hg clone` The difference is that `hg clone` makes a nice `.hg/hgrc` file for you and that `hg clone` will use hardlinks to save space when cloning on the same filesystem. – Martin Geisler Jan 27 '10 at 00:04
  • Richard: The output of `hg help init` is here: http://www.selenic.com/mercurial/hg.1.html#init But maybe that is too terse? Please send a mail to our mailinglist if you need more help! See: http://mercurial.selenic.com/wiki/MailingLists – Martin Geisler Jan 27 '10 at 00:06
  • This is a totally valid approach but for larger repositories won't be the fastest way to do it. – StayOnTarget Dec 17 '18 at 19:45
5

You may use

hg strip revision

to kill any revision and its subtree in your local repository.

https://www.mercurial-scm.org/wiki/Strip

But don't try to use it for anything that has been already pushed.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
averasko
  • 970
  • 7
  • 15
  • You can use it on a public revision, but if you pull again, it will re-appear. (hg strip only works with the local repository.) – Mike Rosoft Feb 27 '19 at 11:44
2

Just delete everything you have on your local system and re-clone the remote repo.

OJ.
  • 28,944
  • 5
  • 56
  • 71
2
hg strip `hg out --template "{rev} {author}\n" | grep YOUR_AUTHOR_NAME | cut -d " " -f 1`

does the trick for me.

It strips all revisions that aren't pushed to the default repository which are created with your author name.

You can also use this style to make it not checking with the default repository but with another Repository

hg strip `hg out OTHER_REPO_ALIAS --template "{rev} {author}\n" | grep YOUR_AUTHOR_NAME | cut -d " " -f 1`
Sebastian
  • 21
  • 1
0

If you are using TortoiseHg, one simple way to get out of a (small) mess is to first update to the latest revision, then select your changesets and initiate "merge with local". When the merge dialogue appears, simply click the little '+' icon to reveal some extra options, one of which is "discard changesets from merge target (other) revision". Doing this will mean your changesets will still be in the repo and get pushed, but will have no effect, because they will be discarded in the merge. If you have a lot of changesets spanning many heads, you might not want to pollute the repo this way, but it's a simple fix and worth considering if the changesets you are discarding contain data that you may later want to reference.

MrFlamey
  • 119
  • 3