views:

67

answers:

3

When using Mercurial, how do you undo all changes in the working directory since the last commit? It seems like this would be a simple thing, but it's escaping me.

For example, let's say I have 4 commits. Then, I make some changes to my code. Then I decide that my changes are bad and I just want to go back to the state of the code at my last commit. So, I think I should do:

hg update 4

with 4 being the revision # of my latest commit. But, Mercurial doesn't change any of the files in my working directory. Why not?

+1  A: 

hg revert is your friend:

hg revert --all 

hg update merges your changes to your current working copy with the target revision. Merging the latest revision with your changed files (=current working copy) results in the same changes that you already have, i.e., it does nothing :-)

If you want to read up on Mercurial, I'd recommend the very awesome tutorial Hg Init.

pableu
pableu: you're exactly right about how `hg update` works when you have changes in your working copy: it merges them into the target revision. When you are already at that target revision, nothing happens. I edited the answer to say this in a more confident way.
Martin Geisler
+5  A: 

hg revert will do the trick.

It will revert you to the last commit.

--all will revert all files.

See the link for the Man Page description of it.

hg update is usually used to refresh your working directory after you pull from a different repo or swap branches. hg up myawesomebranch. It also can be used to revert to a specific version. hg up -r 12.

Paul Nathan
+1  A: 

An alternative solution to hg revert is hg update -C. You can discard your local changes and update to some revision using this single command.

I usually prefer typing hg up -C because it's shorter than hg revert --all --no-backup :)

Andrey Vlasovskikh