Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo an accidental hg strip?

I have accidentally run hg strip, and deleted a stack of commits. I have not done anything in the repo since. Is there a way for me to bring back this stack of commits, to undo the hg strip I just ran?

like image 225
ChristianCuevas Avatar asked Nov 13 '17 22:11

ChristianCuevas


People also ask

How do I undo hg revert?

Caveat: Doing hg remove [file] on an unmodified file removes the file from the filesystem. In this case, hg add [file] fails and to add it back you have to do hg revert [file] . hg add [file] is the correct answer for files that have been modified prior to removal.

How do I revert a commit in TortoiseHg?

In the TortoiseHg client's Commit dialog, right-click the needed file and then click Revert.

How do you Uncommit mercurial?

A simple way to 'uncommit' your last commit is to use hg strip -r -1 -k. In case the link breaks, the documentation mentioned by @phb states: hg rollback Roll back the last transaction (DANGEROUS) (DEPRECATED) Please use 'hg commit --amend' instead of rollback to correct mistakes in the last commit.

What is hg strip?

hg strip removes the changeset and all its descendants from the repository. It will be as if the changes never existed. Be careful when using this on public changesets as it will not remove it from any other repository and you'll get them back next time you pull.


2 Answers

As long as you didn't run the strip with the --no-backup option, the stripped changesets can be found in the repository under .hg\strip-backup. If you sort the directory content by date the latest one is likely the one you need to restore. Restore it with hg unbundle <filename>.

like image 150
Mark Tolonen Avatar answered Sep 19 '22 23:09

Mark Tolonen


It is possible to hg pull from a strip backup file as an alternative to using hg unbundle.

As noted in a comment on another answer to this question, hg unbundle has fewer options and only works with bundles, but can unbundle more than one bundle at a time. Whereas hg pull can pull from a single source (share/web/bundle) and has other options.

Here's an example of using hg pull based on an external post by Isaac Jurado:

Usually the backup is placed in REPO/.hg/strip-backup/. See the example below:

 $ hg glog
 @  changeset:   2:d9f98bd00d5b tip
 |               three
 o  changeset:   1:e1634a4bde50
 |               two
 o  changeset:   0:eb14457d75fa
                 one
 $ hg strip 1
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved

 saved backup bundle to
 /Users/hchapman/ttt/.hg/strip-backup/e1634a4bde50-backup.hg

And then, what one would do to recover those changesets would be:

$ hg pull $(hg root)/.hg/strip-backup/e1634a4bde50-backup.hg
like image 28
StayOnTarget Avatar answered Sep 21 '22 23:09

StayOnTarget