Hey, I want to revert some changesets in Hg but I\'m having a hard time doing so.
I commited some changes by accident, so I wanted to undo that. After trying a littl
Not certain about the git things, but if you haven't pushed, you can use hg rollback
. If you have pushed, there's not really anything decent you can do about it.
As for the current revision, see hg id
.
See hg strip
from the mq
extension.
Mercurial intentionally doesn't make this easy. Mercurial's mindset is that code committed should continue to exist -- that the record of what didn't work out is almost as important as a record of what did. It's better to commit the negation of your work (which is what backout
does) and still have the record than it is to actually discard it.
If, however, you just can't leave that change around (good reasons: it contains a password you can't change, bad reason: It just didn't work out) you have a few options:
hg clone -r LASTCHANGESETYOULIKE oldrepo newrepo
and now newrepo has everything up to and including LASTCHANGESETYOULIKE. Archive oldrepo and rename newrepo to oldrepo.Here is how to do git reset --soft
. Let us assume you have a graph like this:
... --- [A] --- [B] --- [X] --- [-X]
where X
is the bad commit and -X
is the backout you made. You now want to get rid of X
and -X
while leaving the working copy looking like it did when you committed X
. You do
$ hg update B
$ hg revert --all --rev X # working copy now looks just like in X
$ hg strip --force X # get rid of X and -X
If you want, you can make an alias for this with Mercurial 1.7:
[alias]
softreset = !hg update 'p1($1)' &&
hg revert --all --rev $1 &&
hg strip --force $1
The dirty working copy after hg revert
makes it necessary to use hg strip --force
. You use this new command as
$ hg softreset 10
which will remove 10 and any descendents while leaving the changes in 10 in the working copy. You can of course take this further and implement a hg reset
command:
[alias]
reset = !test "$1" = "--hard" && hg strip $2 || hg softreset $2
The biggest problem with these aliases is the poor error handling. An extension written in Python would be much more robust and maintainable -- perhaps you could make such a "reset" extension and publish it on Bitbucket :)
You've got some good answers already, but a couple additional notes: