Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mercurial

People also ask

How to untrack files in hg?

If you see the help for hg rm --help : hg remove [OPTION]... FILE... Schedule the indicated files for removal from the current branch. This command schedules the files to be removed at the next commit.

How to remove file from hg?

Once you decide that a file no longer belongs in your repository, use the hg remove command. This deletes the file, and tells Mercurial to stop tracking it (which will occur at the next commit). A removed file is represented in the output of hg status with a “ R ”.

What is Mercurial changeset?

A changeset (sometimes abbreviated "cset") is an atomic collection of changes to files in a repository. It contains all recorded local modification that lead to a new revision of the repository. A changeset is identified uniquely by a changeset ID. In a single repository, you can identify it using a revision number.


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


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.


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.


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>"

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

hg revert -r .~1 <file>