mathematical.coffee
mathematical.coffee

Reputation: 56915

mercurial: recover from deleted repository after commit, before push

I have a mercurial repository my_project, hosted at bitbucket. Today I made a number of changes and commited them to my local repository, but didn't push them out yet.

I then majorly stuffed up and fatfingered rm -rf my_project (!!!!!).

Is there some way I can retrieve the changes that I committed today, given that I hadn't pushed them out yet? I know a day's worth of commits doesn't sound like much, but it was!

All the other clones I have of this project are only up-to-date to the most recent push (which didn't include today's changes).

cheers.

Upvotes: 1

Views: 215

Answers (3)

Martin Geisler
Martin Geisler

Reputation: 73758

I'm afraid the commit is deleted together with the working copy and file recovery tools are your only option to recover the missing .hg folder. I see you could recover the code from the install — great!

If you're afraid of this happening again, then you could install a crude hook like

[hooks]
post-commit = R=~/backup-repos/$(basename "$PWD");
              (hg init "$R"; hg push -f "$R") > /dev/null 2>&1 || true

That will forcibly push a copy of all your commits to a suitable repo under ~/backup-repos. The -f flag ensures that you will push a backup even if you play with extensions like rebase or mq that modify history. It will also allow pushing changesets from unrelated repos into the same backup repo — imagine two different repos named foo. So the backup repositories will end up with a gigantic pile of changesets after a while and you might want to delete them once in a while.

I tested this briefly and for everyday work I don't think you'll notice the overhead of the extra copy and you might thank yourself later :-)

Upvotes: 1

Chris Morgan
Chris Morgan

Reputation: 90752

No. The changes are only stored in the local repository directory (the .hg directory therein) until you've pushed. They're never put anywhere else (not even /tmp).

There is a possibility that you'll be able to recover the deleted files from the disk, though; search around for instructions and tools for doing that.

Upvotes: 1

Robert Martin
Robert Martin

Reputation: 17157

mercurial cannot save you. The data from mercurial is stored in a hidden directory in the base of your project folder. In your case, probably at my_project/.hg. Your recursive delete would have trashed this folder as well.

So maybe a file recovery tool?

Upvotes: 3

Related Questions