How to find pull-request by a commit sha

Lets have a commit E which is in some unknown pull-request. --ancestry-path option can be used to list all commits between master and E

git log --ancestry-path master ^E

for:

A last commit in master
B Merge pull request #2258 ...
|\
C | some comiit in master
| D onther commit in searched pull-request
| E given commit

it returns:

A last commit in master
B Merge pull request #2258 ...
D onther commit in searched pull-request

If last merge commit from the list is taken we get the merge commit for E with searched pull-request number. To do that we use options:

git log --reverse --grep=\"Merge pull request\" -n 1 --ancestry-path master ^E

which returns only B Merge pull request #2258 the merge commit with searched pull-request number.

Git Alias

For convenience it can be turned into git alias.

logpr = !sh -c 'git log --reverse --grep=\"Merge pull request\" -n 1 \
    --ancestry-path master ^$1 $@' -

Then

$ git logpr 4b6d8f9 --oneline
d2a35bc Merge pull request #2258 from daviddavis/temp_1368456830

Caution

This works only if all pull-requests are merged directly into master.