41

How can I examine a changeset in mercurial without looking up its parent? In mercurial, what's the equivalent of

git show HEAD^

Git-show gives the changeset metadata and the diff as well.

notnoop
  • 58,763
  • 21
  • 123
  • 144

5 Answers5

62

Your question has two parts. First, how to get the metadata and diff for a changeset all at once:

hg log --patch --rev tip

You can shorten the options:

hg log -pr tip

The second part of the question is how to say "the parent changeset of X" without looking it up. For that you can use the parentrevspec extension Martin mentioned.

Once you enable the extension you can do:

hg log -pr tip^

You could add an alias to your ~/.hgrc file if you don't want to retrain your fingers from git's command:

[alias]
show = log -pr

Then you could use:

hg show tip^
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Steve Losh
  • 19,642
  • 2
  • 51
  • 44
  • 2
    Without parentrevspec he could just do "-r -2" to get the changeset before tip, right? – Ry4an Brase Aug 22 '09 at 04:49
  • 2
    Yes, that's true. That doesn't work for something like 'hg show mytag ^' though, right? – Steve Losh Aug 22 '09 at 14:51
  • 2
    In general, for this case, `hg export` is preferred (it lists full commit message, etc.). – tonfa Aug 24 '09 at 15:24
  • 2
    Preferred by who? If you want the full commit message you can add `--verbose`/`-v` to the log command. `log`'s output is much more human readable (the date and node/parent IDs are in a friendlier format) and it supports `--color`, unlike `export`. – Steve Losh Aug 25 '09 at 13:04
  • `hg help revsets` is your friend. You can use `-r 'parents(rev)'` to get all the parents (multiple in the case of merging) or `-r 'p1(rev)'` or `-r 'p2(rev)'` for the first or second parent of a rev. – Cheetah Oct 09 '12 at 22:41
15

I think you want hg export cset.

tonfa
  • 24,151
  • 2
  • 35
  • 41
5

A similar command to "git show HEAD^" would be:

hg log -pr -2   # -1 (last commit), -2 - one before it, etc.

OR

hg exp tip^  # tip^ is similar to -r -2

or for instance if you want to look at the last 3 commits (with diff):

hg log -pr -3:  # colon means start 3 commits behind and up to tip inclusive

A bit to late with the answer, but still. :)

UPDATE: apparently now HG supports git syntax as well:

hg exp tip^^^..tip

or

hg log -pr tip~4
Alex
  • 948
  • 1
  • 13
  • 17
  • 1
    This is not an “exact equivalent”: unlike `git show`, `hg log -pr` only shows the headline of the commit message, not the entire message. To show the entire message, one must also pass `-v`, as in `hg log -pvr REV`. – wchargin Nov 16 '19 at 05:08
  • @wchargin fair enough. Is the answer really bad enough for downvote? What about exp example? – Alex Nov 16 '19 at 08:19
  • I’d be more than happy to remove the downvote if the answer is fixed to not say that they are “exactly equivalent” or “equivalent” when they’re not. :-) – wchargin Nov 16 '19 at 08:31
  • I had a commit whose message was originally one line long, and which I later reworded to add details. I found this answer and ran `hg log -pr -2`, and saw that the message was only one line. As this command was supposed to be *equivalent* to `git show`, it followed that I must be on the wrong commit, so I spent some time trying to find out how that might have happened. (Did I accidentally rebase/evolve something?) In reality, I was never on the wrong commit. I didn’t downvote to “nitpick”; I downvoted because the fact that this answer was incorrect caused me confusion and lost me time. – wchargin Nov 19 '19 at 16:33
  • I still think it's nitpicking. I'm not even sure the `git show` was the same 6 years back. Anyway I've updated the answer to not use the word `equivalent`, to avoid any further confusion. – Alex Nov 19 '19 at 19:35
3

If you just want to see the contents and differential of a commit, use this:

hg diff -c <the commit hash or bookmark name>

To see the commit you've checked out (HEAD in git), do this:

hg diff -c -1

If you want to see the commit before it (HEAD^ in git), do this:

hg diff -c -2

Simple.

Amir Memon
  • 416
  • 3
  • 10
2

You should also take a look at the parentrevspec extension to enable a more Git-like syntax for specifying revisions.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229