How to get a specific version of a file in Mercurial?

I am new to Mercurial. Just cannot find the right command. Tried update/checkout with no luck. I am using local repository. Thanks


Solution 1:

I think you want hg revert -r<rev> <file> (this will change that file to be as it was at the given revision).

Solution 2:

As djc said revert alters a file in place to match a prior revision. If you want it not in place you can use hg cat -r revisionid filename (substituting revisionid and filename of course) which will output the file to stdout, suitable for redirecting anyplace you'd like.

Solution 3:

hg revert does indeed solve this problem. But I think that you are confused about a broader range of things than simply the answer to your question and want to try to answer more fully.

hg update is a whole repository command and will not work on individual files. It is unlike the subversion svn update in this way. If you do hg --help update you can see that this is the case because the command takes no file argument. It can be used to move your whole repository to a particular snapshot, but cannot be used to do that to just one file.

If you type hg --help you see a list of commands. It's a rather large and somewhat daunting list, but if you read through it, you'll find this line:

revert       restore individual files or directories to an earlier state

Now, if you just want the last state for comparison purposes, there is another command you may be interested in, and that's hg cat. That will allow you to print out the contents of a file at any particular revision. You can then redirect its output into some other file. Then you can have the previous known good version of your file and the old version to compare side-by-side.

The reason why Mercurial has a separate update command is that it is possible to do something in Mercurial that is impossible in Subversion. You can update to an earlier version, make changes, then commit. This will create a branch. The update command has the effect of also changing the parent revision of the current working directory as well as changing the contents of all the files in that directory to that parent revision's versions.

That means revert changes the contents of a file (or even the whole repository if you give the command the right arguments) but leaves the parent revision of the current working copy the same.

You can find out the parent revision (or revisions in the case of a merge) of the current working copy by using the hg parents command.

In Subversion revisions are a strictly linear progression. Mercurial creates branches at the drop of a hat, and they are almost as easy to merge. Revisions form a DAG, not a strictly linear progression.

Solution 4:

To extract a specific revision of a specific file you can do this on Windows:

hg cat "<FileToBeExtractedPath>" -r 9 > "<ExtractionPath>"

Here, 9 is the revision number.

Or even better:

hg cat "<FileToBeExtractedPath>" -r 9 -o "<ExtractionPath>"

Solution 5:

Came here trying to get the previous version, so here is the exact command:

hg revert -r .~1 <file>