Find a Pull Request on GitHub where a commit was originally created

GitGithubPull Request

Git Problem Overview


Pull Requests are great for understanding the larger thinking around a change or set of changes made to a repo. Reading pull requests are a great way to quickly "grok" a project as, instead of small atomic changes to the source, you get larger groupings of logical changes. Analogous to organizing the lines in your code into related "stanzas" to make it easier to read.

I find myself looking at a file or a commit, and I wonder if there is a way to backtrack the commit to the Pull Request that originally created it. That Pull Request would have been merged eventually, but not necessary with a merge-commit.

Git Solutions


Solution 1 - Git

You can just go to GitHub and enter the SHA into the search bar, make sure you select the "Issues" link on the left.

UPDATED 13 July 2017

Via the GitHub UI there is a now a really easy way to do this. If you are looking at a commit in the list of commits in a branch in the UI, click on the link to the commit itself. If there is a PR for that commit and it wasn't added directly to the branch, a link to the PR listing the PR number and the branch it went into will be directly under the commit message at the top of the page. enter image description here


Example of finding a PR by clicking on a link to the commit

If you have the commit SHA and nothing else and don't want to go digging around for it, just add /commit/[commit SHA] to the repo url, and you will see the commit page, with the PR link if it exists. For example, if the SHA is 52797a7a3b087231e4e391e11ea861569205aaf4 and the repo is https://github.com/glimmerjs/glimmer-vm , then go to https://github.com/glimmerjs/glimmer-vm/commit/52797a7a3b087231e4e391e11ea861569205aaf4

Solution 2 - Git

git config --add remote.origin.fetch +refs/pull/*/head:refs/remotes/origin/pull/*
git fetch origin
git describe --all  --contains <COMMIT>

If necessary, change origin to the name of the remote that points to the GitHub repository to which the pull request would have been sent. The first command only needs to be run once for any given remote, and the second will generally be done when getting other updates.

This will cause git to get information about pull requests along with actual branches. They'll show up as remote-tracking branches like origin/pull/123. Once that is done, you can use git describe with the --all and --contains options to show the first branch which has the referenced commit.

However, this won't work if the commit you're looking for is actually a modified version of the commit from the pull request such as if the changes were rebased onto other work or the person doing the merge decided to make some changes.

Solution 3 - Git

Since Oct. 13, 2014, this should be straightforward:

For example:

You can see for the file hakimel/reveal.js/plugin/markdown/markdown.js, my contribution now comes with a reference to the PR #734 it originated.

PR from contrib

This comes from Linking merged pull requests from commits:

> We've been including the containing branches and tags on commit pages to give you more context around changes. Now, commits in a repository's default branch will also show you the pull request that introduced them.

commit with PR reference in it!

> In the pull request, you can see the discussion around why the commit was introduced, and get a clearer picture of the reason for the change. > > As always, if you know the commit SHA, you can skip the commit page and search for the pull request directly.

Solution 4 - Git

Put the commit hash into the Pull Request filters field on GitHub.

enter image description here

Solution 5 - Git

I had this same problem and wrote the pr_for_sha bash helper, documented here:

http://joey.aghion.com/find-the-github-pull-request-for-a-commit/

Call it like pr_for_sha <COMMIT> and it will open the corresponding github pull request page in a browser.

Solution 6 - Git

I've been a heavy user of the cheeky little link on the GitHub web UI but wanted a faster way that would take me straight there from the terminal, basically a git pr SHA command. It took a bit of doing, but here's a series of git aliases that will set that up for you on MacOS:

  git config --global alias.merge-commits '!funct() { git log --merges --reverse --oneline --ancestry-path $1..origin | grep "Merge pull request";  }; funct'
  git config --global alias.pr-number '!funct() { git merge-commits $1 | head -n1 | sed -n "s/^.*Merge pull request #\\s*\\([0-9]*\\).*$/\\1/p"; }; funct'
  git config --global alias.web-url '!funct() { git config remote.origin.url | sed -e"s/git@/https:\/\//" -e"s/\.git$//" | sed -E "s/(\/\/[^:]*):/\1\//"; }; funct'
  git config --global alias.pr '!funct() { open "`git web-url`/pull/`git pr-number $1`" ;}; funct'

If you're on Linux, replace open with xdg-open and you're golden. It shouldn't be too difficult to adapt to work with GitLab either.

Note this will only work if you practicing GitHub flow and creating explicit merge commits.

I've written a more detailed explanation of how this all works here: https://tekin.co.uk/2020/06/jump-from-a-git-commit-to-the-pr-in-one-command

Solution 7 - Git

You can also do this using the gh cli, here's an example:

gh pr list --search "30aedc5aaab4708b2144c648a9c7ace9aff4cd31" --state merged --json url --jq '.[0].url'

For more info, see - https://cli.github.com/manual/gh_pr_list

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDragonFaxView Question on Stackoverflow
Solution 1 - GitRustyTomsView Answer on Stackoverflow
Solution 2 - GitqqxView Answer on Stackoverflow
Solution 3 - GitVonCView Answer on Stackoverflow
Solution 4 - GitDan RosenstarkView Answer on Stackoverflow
Solution 5 - GitJoeyView Answer on Stackoverflow
Solution 6 - GittekinView Answer on Stackoverflow
Solution 7 - GitSher ChowdhuryView Answer on Stackoverflow