Search code examples
version-controlmercurialdvcsrevert

Retrieve old version of a file without changing working copy parent


How do you get a copy of an earlier revision of a file in Mercurial without making that the new default working copy of the file in your workspace?

I've found the hg revert command and I think it does what I want but I'm not sure.

I need to get a copy of an earlier revision of my code to work with for a few minutes. But I don't want to disturb the current version which is working fine.

So I was going to do this:

hg revert -r 10 myfile.pls

Is there a way to output it to a different directory so my current working version of the file is not disturbed? Something like:

hg revert -r 10 myfile.pls > c:\temp\dump\myfile_revision10.pls

Solution

  • The cat command can be used to retrieve any revision of a file:

    $ hg cat -r 10 myfile.pls
    

    You can redirect the output to another file with

    $ hg cat -r 10 myfile.pls > old.pls
    

    or by using the --output flag. If you need to do this for several files, then take a look at the archive command, which can do this for an entire project, e.g.,

    $ hg archive -r 10 ../revision-10
    

    This creates the folder revision-10 which contains a snapshot of your repository as it looked in revision 10.

    However, most of the time you should just use the update command to checkout an earlier revision. Update is the command you use to bring the working copy up to date after pulling in new changes, but the command can also be used to make your working copy outdated if needed. So

    $ hg update -r 10   # go back
    (look at your files, test, etc...)
    $ hg update         # go back to the tip