What is the mercurial equivalent to git checkout -
? I want to go to wherever I last was.
I tried hg checkout -
, but that yields a parse error.
What is the mercurial equivalent to git checkout -
? I want to go to wherever I last was.
I tried hg checkout -
, but that yields a parse error.
There isn't one.
The -
here in Git is shorthand for @{-1}
, which refers to the reflog for HEAD
. While Git's branches are much more like Mercurial's bookmarks than anything else, every time you switch branches, Git writes an entry to a reflog saying before switching, the current branch was <name>. A subsequent @{-number}
means scan through this particular reflog looking for such entries, count until you hit the number-th such entry, and use that branch name as the name here. For this to work, the system must maintain this log of checkout
operations, storing the old name whenever a new name is supplied. I'm glossing over Git's so-called detached HEAD mode here: @{-number}
refers to both names and raw hash IDs. Mercurial has nothing like Git's "detached HEAD" mode: you're always on some branch. Mercurial branches are real, solid things, unlike Git's branches, which evaporate while leaving the commits around: In Git, commits are on many branches simultaneously, which is quite nonsensical in Mercurial, but sensible enough in Git, and deleting a branch just takes away one of the many names that allow Git to find the commits.
Since Mercurial does not retain a log of each hg update
, there is no way to retrieve a previous update argument from the log. [Edit: there is the experimental Journal extension; see Max Heiber's comment.] You could of course write your own little wrapper that runs hg update
and, if the update succeeds, logs which commit and/or branch was in use just before the update. This wrapper could then implement the "update back" operation.
Side note: The hg topics
command that StayOnTarget mentioned in a comment is part of the evolve extension. This gives Mercurial the ability to do things Git just can't do. It's not the same as using Git branches at all. Mercurial without evolve can already do the same things Git can do, and a bit more (because Mercurial branchs are real). However, the permanance of branches, while attractive at first blush, is in my opinion often a net negative (by which I mean "you should use bookmarks first, and maybe only"). But the evolve extensions are are a feature sorely missing from both Mercurial and Git; they're available in Mercurial, but not part of the standard distributions and require extra user knowledge (see also the roadmap and its links).