Quantcast
Viewing all articles
Browse latest Browse all 20

Getting started with Mercurial for a small team–Reverting

This is the sixth post in this series.  Need to delete undo a commit?  Want to throw away your work and start over. It happens to all of us.  With Mercurial this is a breeze.

Undo changes before a commit

So your working on a file and realize for whatever reason you need to scrap your work. 

Typing hg stat, shows you haven’t committed your work yet.

Image may be NSFW.
Clik here to view.
image

Great, to undo our work let’s move into the directory where the file is just to make our task a little easier.

Next, we'll type hg revert <filename.extension>.  In my particular case, hg revert Index.cshtml.

You don’t get any message but now if you take a look at what you have in your directory you’ll see two files.  Not perhaps what one would expect.  Mercurial doesn’t delete or overwrite the file with what is in the repository.  Instead it adds the .orig extension to the file you don’t want.

Image may be NSFW.
Clik here to view.
image

This is just for extra safety.  However, you can override this by adding --no-backup to the command (hg revert --no-backup Index.cshtml).

If you do this in Tortoise Hg, it automatically runs with the --no-backup option. You can change that by unchecking the checkbox "do not save backup files" in the lower left hand corner.

To finish up you can delete the Index.cshtml.org and run hg stat to see that your back in order.

Image may be NSFW.
Clik here to view.
image

If you edited this in Visual Studio, you’ll get prompted by VS to reload the project.

Image may be NSFW.
Clik here to view.
image

Selecting yes, you’ll see your changes gone and back to what it is in the repository.

To review, the steps to revert a file that hasn’t been committed yet are:

1. hg stat (to view your current state)
2. hg revert <filename.extension>
3. Delete the .orig file (if you don’t use --no-backup)
4. hg stat (to make sure everything is taken care of)

Undoing a commit

Already committed your work?  Let’s undo this mishap now.  Now being the keyword.  With hg rollback you can roll back the last commit in the repository.

Going back to our above sample, let’s make a change to a file and commit it.

Image may be NSFW.
Clik here to view.
image

I ran hg stat, after my commit just to make sure we are clear of any other changes.  I also ran a new command called hg tip to show that my last commit is indeed the one I’m targeting to revert.

If we are confident this is the commit we want to revert we are ready to rollback.

Type hg rollback

Image may be NSFW.
Clik here to view.
image

It gives a message back that the tip has been rolled back and the working directory is back to our revision number for the last commit.  However, we are not done.  If you type hg stat you’ll notice our file is in a modified state.

Image may be NSFW.
Clik here to view.
image

The file is in the state we left it with our changes before we committed it.  We’ll need to revert our work.

Image may be NSFW.
Clik here to view.
image

This time I used the --no-backup option on the revert to avoid having to delete the .orig file.

As always run hg stat to ensure everything looks right.

To review the steps to undo the last commit:

1. hg stat
2. hg tip
3. hg rollback
4. hg stat
5. hg revert --no-backup <filename.extension>
6. hg stat

Undo an older Commit?

So what if you have to undo a commit that is more than one revision back.  Well, that’s a more advanced topic than what I’m covering.  Take a look at Chapter 9. Finding and fixing mistakes in Mercurial: The Definitive Guide.

In the next post we’re going to go over branching

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 20

Trending Articles