开发者

How can I recover a removed file in Mercurial (if at all)?

Accidentally, by using a GUI as opposed to CLI, I removed every file in a Mercurial project.

I recovered with Revert ok and lost some work, which as I have time machine I could easily get back. But is there a way of un-remove/undelete such files? Trawled through the manual and googled but cannot see anything. Any plugins?

I am probably answering my own question here but the files were gone from the directory and were not in the trash to recover so I am assuming Remove is irrevocable?

p.s. I know that hg forget or hg remove -Af will remove without deleting from the directory but my question has to do w开发者_运维技巧ith the error I made as opposed to cool thinking the action through.


First, use hg grep to find the deleted file you wish to recover. The output of this command will show you the last revision for which the file was present, and the path to the deleted file. Second, run hg revert -r <revision number> <path to deleted file> The deleted file will now be in your working copy, ready to be committed back into head.


Quote from comment:

I set up a repository, committed all, Removed and then committed again

If this is the case then you just need to update the working directory to the previous revision:

$ hg update -C -r-2

Note the negative revision number. If the files you deleted aren't in the previous revision, you can find them by using:

$ hg log -v


For Mercurial 1.6 and above

If you know the name of the delete file you can find its revision easily with:

hg log -r "removes('NAME.c')"

This will give you the revision in witch a file called NAME.c (in the root) is deleted.

Then you can revert the file to the previous revision with (like other answers):

hg revert -r <revision number> <path to deleted file>

You can use a file name pattern instead to adapt to what you know, for example you can use **/NAME.c to search in all directories. You can read about it in File Name Patters. And use this link to know about the new revset specifications.


Well this worked for me.

hg revert -r revision pathToTheFile


An addition to the accepted answer - this is faster if you want to undo all removals in a commit. I deleted a large folder with a few hundred files in it and did hg addremove, which was not at all my intent, so had to undo all of those deletes.

Using Find deleted files in Mercurial repository history, quickly? + xargs + tr, revert all revision -3 removals to the version from revision -4:

hg log -r -3 --template "{rev}: {file_dels}\n" | tr ' ' '\n' | xargs hg revert -r -4 

Note that this will fail if any of your files have spaces in the name; http://hgbook.red-bean.com/read/customizing-the-output-of-mercurial.html doesn't appear to have any templates where {file_dels} is split by \n at the moment.


You can undo the last commit on a repo with hg rollback. There's only one level of rollback available, so if you did the remove with more than one commit, this won't completely undo your change. This only works on your local repository, so if you've pushed you won't be able to undo it in the remote repo.


You can remove committed revisions using the hg strip command, which is provided by the mq (Mercurial Queues) extension. This should give you back your files.

Make a backup before trying that out, because it will alter Mercurial's database of changesets.


The following worked for me.

hg revert -r <Revision Number> <File Name>

(Optional, to revert all files)

hg revert -r <Revision Number> --all


The below method is straightforward and so stupid that it cannot go wrong. If you have deleted or renamed multiple files, it will be ok.

hg clone mydirectory mydirectory1

and now you start mc (or Far Manager) and compare what it was vs what it has become.

when it's done, just delete mydirectory1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜