How to Retrieve The Oldest Commit of a GitHub User

Amit Merchant · December 6, 2021 ·

While building Your First Commit Ever, I needed to retrieve the oldest commit of a GitHub user. After some Googling, I found a GitHub API endpoint that can be used to accomplish this.

The API Endpoint

Essentially, there’s an endpoint to search commits for a specific user.

GET /search/commits

This endpoint along with a few filters through the query parameters would return a response consisting of the oldest 30 commits.

Here’s how the endpoint would look like after applying these filters.

GET /search/commits?q=author:amitmerchant1990&order=asc&sort=committer-date

As you can tell, the first query parameter q with author targets the specific GitHub user for whom you want to retrieve the commits.

The second and third parameters, order and sort respectively, would sort the commits based on the commit date in the ascending order.

Using The Endpoint

This endpoint can then be called as a GET request. Here’s a snippet from my app in which I’m calling it through jQuery Ajax.

function fetchUserCommits(user) {
    $.ajax({
        url: "https://api.github.com/search/commits?q=author:" + user + "&order=asc&sort=committer-date",
        type: "get",
        headers: {
            'Accept': 'application/vnd.github.cloak-preview'
        },
        dataType: 'json',
        success: function (data) {
            console.log(data);
        },
        error: function () {
            console.log('No GitHub user is available of this username.');
        }
    });
}

Here’s how the response (data) returned from the API for a valid GitHub user looks like.

And from here, you can pick the oldest commit by using data.items[0] and retrieve all the important detail about that commit like commit date, commit repository, commit message, and so on.

👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!

Comments?