UW AMath High Performance Scientific Computing
 
AMath 483/583 Class Notes
 
Spring Quarter, 2011

Table Of Contents

Previous topic

Mercurial (hg)

Next topic

Bitbucket repositories: viewing changesets, issue tracking

This Page

More commands in hg

Reverting to a previous version

This is a continuation of the example in Creating your own Bitbucket repository.

  1. Now try the following:

    $ rm -f testfile.txt
    $ hg status
    ! testfile.txt

    The first line removes this file (the -f flag forces this without prompting the user to make sure that’s what’s intended, since removing files can be dangerous).

    The “hg status” command now shows ! next to the file name, indicating that a file under version control has disappeared.

    Ordinarily if you remove a file in Unix it’s gone. Period. But with version control, we can easily recover from such a blunder:

    $ hg revert testfile.txt
    $ hg status --all
    C testfile.txt

    The “hg revert” has restored this file using the most recent version committed. You can also use “hg revert” if you make some changes to a file and then decide they were a bad idea.

    Not only can you revert to the most recent version (in the tip), you can revert to any previous version that was committed.

    Try this:

    $ hg revert -r 0 testfile.txt
    $ hg status
    M testfile.txt
    
    $ hg diff testfile.txt
    diff -r 11d71622c220 testfile.txt
    --- a/testfile.txt      Tue Mar 02 23:33:52 2010 -0800
    +++ b/testfile.txt      Wed Mar 03 00:01:29 2010 -0800
    @@ -1,3 +1,2 @@ This is a new file
     This is a new file
     with only two lines so far
    -Adding a third line

    Here we have reverted to the version in changeset 0 (the first time we committed, when the file only had two lines). Now “hg status” shows that it is modified (relative to the tip, which has three lines) and “hg diff” shows the change relative to the tip: the third line was removed.

  2. Revert back to the tip:

    $ hg revert --no-backup testfile.txt

    The file should now have 3 lines again. If the –no-backup flag is omitted, Mercurial will create a file testfile.txt.orig with the modified version, just in case you regret your revert. (Not all revision control systems have this feature!)

Further reading