Find a commit on GitHub given the commit hash

210,383

Solution 1

A URL of the form https://github.com/<owner>/<project>/commit/<hash> will show you the changes introduced in that commit. For example here's a recent bugfix I made to one of my projects on GitHub:

https://github.com/jerith666/git-graph/commit/35e32b6a00dec02ae7d7c45c6b7106779a124685

You can also shorten the hash to any unique prefix, like so:

https://github.com/jerith666/git-graph/commit/35e32b


I know you just asked about GitHub, but for completeness: If you have the repository checked out, from the command line, you can achieve basically the same thing with either of these commands (unique prefixes work here too):

git show 35e32b6a00dec02ae7d7c45c6b7106779a124685
git log -p -1 35e32b6a00dec02ae7d7c45c6b7106779a124685

Note: If you shorten the commit hash too far, the command line gives you a helpful disambiguation message, but GitHub will just return a 404.

Solution 2

View single commit:
https://github.com/<user>/<project>/commit/<hash>

View log:
https://github.com/<user>/<project>/commits/<hash>

View full repo:
https://github.com/<user>/<project>/tree/<hash>

<hash> can be any length as long as it is unique.

Solution 3

The ability to search commits has recently been added to GitHub.

To search for a hash, just enter at least the first 7 characters in the search box. Then on the results page, click the "Commits" tab to see matching commits (but only on the default branch, usually master), or the "Issues" tab to see pull requests containing the commit.

To be more explicit you can add the hash: prefix to the search, but it's not really necessary.

There is also a REST API (at the time of writing it is still in preview).

Share:
210,383
dopplesoldner
Author by

dopplesoldner

Updated on July 24, 2022

Comments

  • dopplesoldner
    dopplesoldner almost 2 years

    I am fairly new to Github and have come across an amateur-ish problem.

    I have been asked to do a code review and have been provided with a commit hash, however I have tried looking in Git if I can search using commit hashes but couldn't find anything.

    Is there a way I can find the changed code just by using the commit hash?

  • RubyTuesdayDONO
    RubyTuesdayDONO over 11 years
    i came across this when trying to trace an assertion in mongo, and found that there's a similar URL pattern to view a specific file, given the hash of a commit: github.com/$owner/$project/blob/$hash/path/to/file.ext - e.g. github.com/mongodb/mongo/blob/…
  • Rafael Barros
    Rafael Barros over 10 years
    In this: git log -p -1 35e32b6a00dec02ae7d7c45c6b7106779a124685, the -1 is necessary because otherwise it would show all the olders commits; it's good to know that you can use the four initial numbers of the hash (the minimum in my tests), because there’s no auto completion for the hash; and you can't specify the branch like this: git log master -p -1 35e3. Git version: 1.7.9.5.
  • Rafael Barros
    Rafael Barros over 10 years
    One more obs, but one very important: again, you can't specify a branch, but it automatically search the local and remote branchs when you give a hash. So, yes you can search for a specific remote diff before merging to the local repo by the command line.
  • SimplGy
    SimplGy almost 10 years
    In case anyone's wondering (I was!), this also works with the first 8 of the hash both on github: github.com/jerith666/git-graph/commit/35e32b6a and on the command line: git log -p -1 35e32b6a
  • Ed Avis
    Ed Avis almost 10 years
    Isn't there a way to search for that hash in all repositories on Github?
  • Matt McHenry
    Matt McHenry almost 10 years
    @EdAvis, I did some quick searching and tried some obvious things, like putting the sha1 into the general github search field, and couldn't find a way to do that. In general a commit could be in more than one repository, and some or all of the repositories that contain it might not be public.
  • Ed Avis
    Ed Avis over 9 years
    Yup, it appears you can search issues by commit hash <github.com/search?q=e1109ab&type=Issues> but not code. I've logged a feature request.
  • Ed Avis
    Ed Avis over 9 years
    I used the contact form and a member of Github staff replied to say they had added it to their list of possible features. They didn't give a bug number or other way to track the request.
  • ocroquette
    ocroquette over 9 years
    It's kind of dumb that the UI doesn't make this easier than it is... I hope the feature request will make it.
  • Kasun Siyambalapitiya
    Kasun Siyambalapitiya over 7 years
    if the length is at least 7 characters it is ok
  • qwertzguy
    qwertzguy over 7 years
    No minimum length for the commit hash is 4 characters (again, as long as it is unique in the entire repository)
  • summerian
    summerian about 7 years
    I am surprised that it actually works this way. It's so not intuitive. By default Github will show the "Code" tab, with obviously no results in it. Shouldn't it show the only tab with any results in it by default?
  • Greg Tarsa
    Greg Tarsa almost 7 years
    The UI now supports this. Do a search for hash:<sha> and Voila!
  • Matt McHenry
    Matt McHenry almost 7 years
    Hm ... only the full hash, though ... bummer.
  • Brad Parks
    Brad Parks over 4 years
    So to be clear, if you have your own enterprise install of github, you can find any commit in any repo by searching for it like so: https://YourGithubDomain/search?q=YOUR_COMMIT_HASH&type=Comm‌​its Note that I tried this on Github as well, and it worked there too e.g. https://github.com/search?q=38db172d13962ea177c00c9a3b4b3169‌​b317e94b&type=Commit‌​s
  • Darkhydro
    Darkhydro almost 3 years
    @Todd I haven't been able to get this working once in our repo. The other solution using the URL works great though.
  • Neman
    Neman over 2 years
    Seems kind of crazy in 2022 we still can't search for a hash across all branches using GitHub.com search. The URL-based solution in the accepted answer is clunky but will likely work reliably until the end of time.