1

I am trying to retrieve the last commit hash for a particular file on github.

I want the last commit hash of this file https://github.com/publicsuffix/list/blob/master/public_suffix_list.dat

The following link gives me the list of all files that were modified. https://api.github.com/repos/publicsuffix/list/commits/master

I can find the commit hash by checking the files object to see if the required file was part of the last commit.

 "files": [
    {
      "sha": "6a4c772093e1c7694627463ef05a99be98ab355b",
      "filename": "public_suffix_list.dat",
      "status": "modified",
      "additions": 1,
      "deletions": 0,
      "changes": 1,
      "blob_url": "https://github.com/publicsuffix/list/blob/6f03f42a65d006c8ae657f125f14fb8f9d3337f4/public_suffix_list.dat",
      "raw_url": "https://github.com/publicsuffix/list/raw/6f03f42a65d006c8ae657f125f14fb8f9d3337f4/public_suffix_list.dat",
      "contents_url": "https://api.github.com/repos/publicsuffix/list/contents/public_suffix_list.dat?ref=6f03f42a65d006c8ae657f125f14fb8f9d3337f4",
      "patch": "@@ -11793,6 +11793,7 @@ goip.de\n // Submitted by Eduardo Vela <evn@google.com>\n run.app\n a.run.app\n+web.app\n *.0emm.com\n appspot.com\n blogspot.ae"
    }
  ]
jthill
  • 55,082
  • 5
  • 77
  • 137
Arpit Bharti
  • 111
  • 1
  • 2
  • Do you [mean this: 6f03f42a65d006c8ae657f125f14fb8f9d3337f4](https://github.com/publicsuffix/list/commits/master/public_suffix_list.dat)? I just pressed "History" button on your first link. – Marek R Jun 06 '19 at 13:13
  • If you don't want to do this with Git, but only with GitHub API operations, omit the [tag:git] tag. – torek Jun 06 '19 at 16:04

1 Answers1

0
git log <project_dir>/<file_location>/<file_name>

Since you want the most recent commit, use

git log -n 1 --pretty=format:%H -- <project_dir>/<file_location>/<file_name>

EDIT: Because @phd corrected me in comment section of this this answer.

Please refer Github API doc and create the URL like this

https://api.github.com/repos/<github_handle>/<repo_name>/commits?path=>file_path>

https://api.github.com/repos/shravan40/tiny_url/commits?path=README.md

Shravan40
  • 8,922
  • 6
  • 28
  • 48