Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

How to Retrieve The Oldest Commit of a GitHub User

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.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?