larsks
larsks

Reputation: 312203

Referring to the commit prior to a commit specified using `:/` syntax?

The most recent commit whose commit message contains "foo" is spelled :/foo, as in:

git show :/foo

How does one refer to the parent of that commit? :/foo^ is incorrect; that results in:

fatal: ambiguous argument ':/foo^': unknown revision or path not in the working tree.

The only thing I've been able to come up with so far is using the output of git rev-parse:

git show $(git rev-parse :/foo)^

...which works, but seems needlessly complex.

Upvotes: 2

Views: 27

Answers (1)

torek
torek

Reputation: 489253

If :/foo finds HEAD:^{/foo}, you can put the trailing ^ into the second syntax as HEAD:^{/foo}^. If it finds xyz:^{/foo}, you can put the trailing ^ in this way. But since it might find either of those, or some other such string, there is no one-step syntax for what you want.

The two-step syntax is what is actually used in various Git scripts, though typically it is coded more as:

hash=$(git rev-parse "$usersupplied") || exit
hash=$(git rev-parse $hash^) || exit

so as to handle errors better.

Upvotes: 2

Related Questions