GitHub - Lists References using the REST API

by
Jeremy Canfield |
Updated: February 01 2025
| GitHub articles
Let's say the git log command returns something like this. Notice in this example that the latest commit has SHA-1 mks910122020slsmm3lsosos020399489sl.
commit mks910122020slsmm3lsosos020399489sl
Author: Jack Doe <jack.doe@example.com>
Date: Wed May 31 14:51:14 2020 -0500
Third commit
commit dkci85474fjfdkd9393934k49f9fk002kd01
Author: Jane Doe <jane.doe@example.com>
Date: Tue May 30 18:23:36 2020 -0500
Second commit
commit fj83m3ld0d0d3m3ld0389303l3ld0d0d39dl
Author: John Doe <john.doe@example.com>
Date: Mon May 29 20:26:09 2020 -0500
First commit
And .git/refs/heads/<default branch name> should contain the SHA-1 of the latest commit.
~]$ cat .git/refs/heads/main
mks910122020slsmm3lsosos020399489sl
Here is an example of how to list the references using curl.
curl \
--request GET \
--location \
--header "Accept: application/vnd.github+json" \
--header "Authorization: Bearer <your token>" \
--header "X-GitHub-Api-Version: 2022-11-28" \
--url "https://api.github.com/repos/<owner>/<repo>/git/ref/heads/<default branch name>"
For example.
curl \
--request GET \
--location \
--header "Accept: application/vnd.github+json" \
--header "Authorization: Bearer abc123" \
--header "X-GitHub-Api-Version: 2022-11-28" \
--url "https://api.github.com/repos/foo/bar/git/ref/heads/main"
Something like this should be returned.
{
"ref": "refs/heads/main",
"node_id": "REF_kwDONtYRc69yZWZzL2hlYWRzL21haW4",
"url": "https://api.github.com/repos/foo/bar/git/refs/heads/main",
"object": {
"sha": "mks910122020slsmm3lsosos020399489sl",
"type": "commit",
"url": "https://api.github.com/repos/foo/bar/git/commits/70da4f4e1b9011c1ae50c7ce2ab4ea1af34c0e43"
}
}
Did you find this article helpful?
If so, consider buying me a coffee over at