11

In git it's quite convenient to identify a commit relative to the latest commit in the repo with HEAD~1.

I have searched and cannot find an equivalent for this in mercurial. I find mercurials revision numbers rather annoying.

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
Benbob
  • 13,876
  • 18
  • 79
  • 114

3 Answers3

11

The correct answer is .^ or .~1.

tip points to the latest revision that entered the repository, not the current revision you're on. Any answers that include tip in them are incorrect.

Mercurial's revset syntax is specified in more detail here: https://www.mercurial-scm.org/repo/hg/help/revsets

x^n: The nth parent of x, n == 0, 1, or 2. For n == 0, x; for n == 1, the first parent of each changeset in x; for n == 2, the second parent of changeset in x.

x~n: The nth first ancestor of x; "x~0" is x; "x~3" is "x^^^". For n < 0, the nth unambiguous descendant of x.

x^: Equivalent to "x^1", the first parent of each changeset in x.

Community
  • 1
  • 1
rain
  • 366
  • 7
  • 6
10

The revset feature of Mercurial is extremely powerful (and much less arcane than git revision specification syntax): see hg help revsets (or online at: http://www.selenic.com/mercurial/hg.1.html#specifying-revision-sets).

See here for a list of predicates (I don't know why they aren't displayed in the online doc): http://hg.intevation.org/mercurial/crew/file/e597ef52a7c2/mercurial/revset.py#l811

In your case that would be: p1(tip).

tonfa
  • 24,151
  • 2
  • 35
  • 41
  • 1
    Since question author has better knowledge of git syntax, I believe my answer is more useful. Still +1 since I didn't know about revsets in mercurial, and now that I've seen few examples , I prefer it to git syntax. – bbaja42 Jun 09 '11 at 13:07
6

There is a mercurial extension that adds git like commands.
Specific command is hg log -pr .^1.

For extra information, see examining a changeset in hg

Edit: Use .^1, not tip^1. As mentioned below, tip gives the most recent commit in the entire repo, which is possibly not what you want. The . is closer in meaning to git's HEAD. (See also: Specify dot as a revision in Mercurial)

ahiijny
  • 351
  • 1
  • 6
  • 21
bbaja42
  • 2,099
  • 18
  • 34
  • 2
    These days, the relative revset syntax is supported by default. It's also useful to know that `p1()` will give the parent of the current working directory, while `tip` will give the latest commit to the whole repo (which may have been on a different branch) – ncoghlan Nov 24 '14 at 10:17