how to restore file deleted with "update -C"

1,142 views
Skip to first unread message

plumo

unread,
Feb 14, 2011, 5:07:16 AM2/14/11
to merc...@selenic.com

hello,

I have accidentally deleted a file with mercurial and am wondering if it is
possible to restore it.
Here is my log:

$ hg add main.py

$ hg commit -m "message"

$ hg push
abort: push creates new remote heads!
(did you forget to merge? use push -f to force)

$ hg pull
added 1 changesets with 1 changes to 1 files (+1 heads)
(run 'hg heads' to see heads, 'hg merge' to merge)

$ hg update
abort: crosses branches (use 'hg merge' or 'hg update -C')

$ hg merge
abort: outstanding uncommitted changes

$ hg update -C
removing main.py
0 files updated, 0 files merged, 1 files removed, 0 files unresolved


Now my main.py file is deleted. Not just removed from repository but deleted
from file system! Very frustrating!
Is there any way to restore it from mercurial's meta data?

Richard
--
View this message in context: http://mercurial.808500.n3.nabble.com/how-to-restore-file-deleted-with-update-C-tp2491483p2491483.html
Sent from the General mailing list archive at Nabble.com.
_______________________________________________
Mercurial mailing list
Merc...@selenic.com
http://selenic.com/mailman/listinfo/mercurial

Lasse Vågsæther Karlsen

unread,
Feb 14, 2011, 6:20:31 AM2/14/11
to Mercurial Mailing List
On Mon, Feb 14, 2011 at 11:07, plumo <rich...@gmail.com> wrote:

hello,

I have accidentally deleted a file with mercurial and am wondering if it is
possible to restore it.
Here is my log:
... 

$ hg update -C
removing main.py
0 files updated, 0 files merged, 1 files removed, 0 files unresolved


Now my main.py file is deleted. Not just removed from repository but deleted
from file system! Very frustrating!
Is there any way to restore it from mercurial's meta data?


If the file was not tracked by Mercurial at this point, no, there is no way through Mercurial to get the file back.

This is why it's never a good idea to start issuing merge commands with uncommitted local modifications, and wield the -C option on the update command with care.

What you should have done when you got the message about uncommitted changes is to issue a "hg status" command to see what those changes were, and then decide what to do with those, before you continue.

"hg update -C" *specifically* says "discard uncommitted changes (no backup)" in the help text.

If you don't have any way of restoring that file outside of Mercurial, like file system backup or similar, then that file is gone.

Robert Bauck Hamar

unread,
Feb 14, 2011, 6:55:13 AM2/14/11
to merc...@selenic.com
*Lasse Vågsæther Karlsen:

>On Mon, Feb 14, 2011 at 11:07, plumo <rich...@gmail.com> wrote:
>
>>
>> hello,
>>
>> I have accidentally deleted a file with mercurial and am wondering if it is
>> possible to restore it.
>> Here is my log:
>>
>...
>
>>
>> $ hg update -C
>> removing main.py
>> 0 files updated, 0 files merged, 1 files removed, 0 files unresolved
>>
>>
>> Now my main.py file is deleted. Not just removed from repository but
>> deleted
>> from file system! Very frustrating!
>> Is there any way to restore it from mercurial's meta data?
>>
>If the file was not tracked by Mercurial at this point, no, there is no way
>through Mercurial to get the file back.
>
>"hg update -C" *specifically* says "discard uncommitted changes (no backup)"
>in the help text.

Fortunately, not even hg update -C will touch untracked files (unless
the revision we're updating to has a file with the same name). The fact
that main.py was removed, indicates that not all hope is gone.

--
Robert Bauck Hamar
USIT/SAPP/GT - Cerebrum
http://www.uio.no/sok?person=hamar

Robert Bauck Hamar

unread,
Feb 14, 2011, 6:49:23 AM2/14/11
to plumo, merc...@selenic.com
*plumo:

>I have accidentally deleted a file with mercurial and am wondering if it is
>possible to restore it.
>Here is my log:
>
>$ hg add main.py
>
>$ hg commit -m "message"
>
>$ hg push
>abort: push creates new remote heads!
>(did you forget to merge? use push -f to force)
>
>$ hg pull
>added 1 changesets with 1 changes to 1 files (+1 heads)
>(run 'hg heads' to see heads, 'hg merge' to merge)
>
>$ hg update
>abort: crosses branches (use 'hg merge' or 'hg update -C')

Note: This is not saying you should do this without knowing what the
commands do.

>$ hg merge
>abort: outstanding uncommitted changes

Outstandning uncommitted changes? Is there something you did that you
did not tell us between here and the "hg commit" above?

>$ hg update -C
>removing main.py
>0 files updated, 0 files merged, 1 files removed, 0 files unresolved
>
>
>Now my main.py file is deleted. Not just removed from repository but deleted
>from file system! Very frustrating!
>Is there any way to restore it from mercurial's meta data?

Yes.

$ hg heads

Will list a number of heads, one of which is your last revision
containing the newly created main.py. "hg update" can take you back to
that revision.

plumo

unread,
Feb 14, 2011, 8:01:23 AM2/14/11
to merc...@selenic.com

thanks for the heads hint. I found the file was being maintained and I was
able to merge the changes back. That will save me a few hours.
--
View this message in context: http://mercurial.808500.n3.nabble.com/how-to-restore-file-deleted-with-update-C-tp2491483p2492393.html
Reply all
Reply to author
Forward
0 new messages