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?
4 Answers 4
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.
10 Comments
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.git log -p -1 35e32b6ahash:<sha> and Voila!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.
2 Comments
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).
6 Comments
https://YourGithubDomain/search?q=YOUR_COMMIT_HASH&type=Commits Note that I tried this on Github as well, and it worked there too e.g. https://github.com/search?q=38db172d13962ea177c00c9a3b4b3169b317e94b&type=Commitsgit show -s <hash> within the repo on my local.With the GitHub CLI gh v2.22.0 (Jan. 2023), you can search from within your local cloned GitHub repository:
See gh search commits:
Examples:
# search commits matching hash "8dd03144ffdc6c0d486d6b705f9c7fba871ee7c3"
$ gh search commits --hash=8dd03144ffdc6c0d486d6b705f9c7fba871ee7c3
But also:
# search commits matching set of keywords "readme" and "typo"
$ gh search commits readme typo
# search commits matching phrase "bug fix"
$ gh search commits "bug fix"
# search commits committed by user "monalisa"
$ gh search commits --committer=monalisa
hash:<sha>since 2017 via ⬇️