Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"bzr uncommit" equivalent in Mercurial?

Bazaar has a wonderful uncommit command, which simply undoes the last commit. Is there any equivalent in Mercurial?

Edit: Bazaar's uncommit command does not modify files – it removes the last commit and associated data (useful, for example, when you notice too late that there is a typo in a commit message or a file wasn't added that should have been).

For example:

$ bzr ci -m "Fixed a proooblem" <-- problem is misspelt
$ bzr uncommit
...
$ bzr ci -m "Fixed a problem" <-- Exactly as if the first commit was correct.
like image 483
David Wolever Avatar asked May 28 '09 01:05

David Wolever


1 Answers

You want the hg rollback command, but see below if you're using Mercurial 2.2 or later.

The rollback command will remove the last transaction from your repository. A commit is a transaction, so you can use this as

% hg commit -m 'My elaburate bugfix.' foo.c foo.h
% hg rollback
% hg commit -m 'My elaborate bugfix.' foo.c foo.h

After the rollback the files will again be seen as modified, and this means that the second commit will store the same changes as the first, but with a better commit message.

Beware: hg rollback is more powerful than a simple "uncommit" function and you can use it to throw away work if you are not careful. To throw away a commit do

$ hg commit -m 'My big and very difficult bugfix'
$ hg pull --update
$ hg rollback

You've now lost the last commit you made and since you updated the working copy to some other revision, the changes in that commit are gone. So you should only use hg rollback to undo a commit if you're certain that hg commit really was the last command that operated on the working copy.

Also, if you have not given the commit message on the command line, then you cannot just press up-arrow twice to redo the commit after a rollback. However, Mercurial 1.5 and later will save your last commit message in .hg/last-message.txt so that you can always find it again after a rollback.

Mercurial 2.2 has a new --amend flag for hg commit. This let's you amend the last commit with new changes. It simply incorporates the changes listed by hg status into the parent commit, as if you had rolled back and committed again.

like image 71
Martin Geisler Avatar answered Oct 30 '22 19:10

Martin Geisler