Undo an accidental hg strip?
Asked Answered
P

3

21

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?

Pragmatics answered 13/11, 2017 at 22:17 Comment(0)
M
32

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

Merill answered 14/11, 2017 at 6:13 Comment(2)
I heart you. That bit of info saved hours of redoing work.Singleton
Wow! You save me for a second time! Can't thank you enough!Erma
S
2

Here is a worked example of unbundle from an external post. I've cleaned it up slightly to make it a little more general:

Recovering stripped files when using Mercurial

If you accidentally strip a patch and do not have a backup for it, you can still recover your files using Mercurial. To recover your files:

Open a Microsoft Windows Command Prompt window.

Navigate to the project folder where you stripped the files.

Run the dir command

Navigate to the .hg folder where Mercurial stores all relevant project files.

Run the dir command again.

Navigate to the strip-backup folder where Mercurial stores the backup bundles of stripped patches.

Run the dir command again. Multiple files display in the directory that use the <hash>-hg format. They are the backup bundles of stripped patches.

enter image description here

Use Windows Explorer to find the required file. Open the strip-backup folder in Windows Explorer, and sort by Date modified descending. Unless the necessary backup bundle is already known, [it is recommended to] restore the bundles in reverse chronological order starting from the most recent bundle.

enter image description here

Navigate back to the project folder.

To restore a bundle, run hg unbundle .hg\strip-backup\<bundle_file_name>. ... You may want to add it to the PATH environment variable to make it accessible globally.

enter image description here

Synchronize the project [using hg pull] to see the restored patch. If the restored patch is not the one needed, then continue restoring the patches in reverse chronological order until the required patch is retrieved.

Note: You may restore the backup bundles in any order, instead of using reverse chronological order. However, it may not be safe to do so. You may end up attempting to restore a backup bundle, which has a dependency on another backup bundle that has not been restored. In this case, you will get an error.

enter image description here

Shillong answered 13/3, 2019 at 11:48 Comment(0)
S
2

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
Shillong answered 13/3, 2019 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.