125

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

1
  • @Omnifarious: I committed a stable version of a file. Then continued to work on the file but soon realized I need the last "good" version. This can be done by update in svn but when I try hg update, it says "0 files ..."
    – Codism
    Commented Jan 21, 2010 at 13:27

5 Answers 5

167

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

4
  • Yep, that's exactly what he wants. :-) Commented Jan 21, 2010 at 17:10
  • 10
    or just hg revert <filename> if you want to discard your current changes, and get the latest that was commited Commented Sep 15, 2013 at 7:03
  • @BlackTigerX Without the revision a non-modified file will not be reverted to anything, even if it is not the most recently committed version. I sometimes use this to 'cherry pick' files between revisions, so such is important. Commented May 11, 2016 at 15:43
  • This is one of the rare Mercurial commands where I feel like Git's UI is better. Git uses "checkout" for this operaion. If I'm updating a file to the version that exists in another branch, I'm not necessarily "reverting" it. Commented Nov 23, 2021 at 18:50
92

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.

2
  • 5
    purrrfect, exactly what I wished for.
    – yota
    Commented Mar 19, 2015 at 7:06
  • Awesome, it's always nice to hear something like that. Commented Mar 19, 2015 at 13:04
26

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.

13

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

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

hg revert -r .~1 <file>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.