Search⌘ K
AI Features

Commits

Explore how to manage GitHub commits through API calls in JavaScript. Understand how to list all commits, retrieve specific commits by SHA, and compare commits to track changes in your repository branches effectively.

Overview

Commits are a version history of our repository. Snapshots of our repository are taken from time to time, which show how the changes were made. Whenever we make any logical changes, we should make a new commit. Authors and timestamps are also included in the commits alongside the message and content. Git commits are made in the branch from which we are checked out. Therefore, it’s recommended to check git status before running a commit. Any new changes should be staged before running a commit.

List commits

First, let's try to fetch all commits from the specified repository:

Request Parameters

Parameters

Type

Description

username

String

The username of the repository owner

repository

String

The name of the repository whose commits are to be listed

Javascript (babel-node)
const endpointUrl = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/commits';
const headers = {
Authorization: 'token {{ACCESS_TOKEN}}',
Accept: 'application/vnd.github.v3+json',
};
const options = {
method: 'GET',
headers,
};
async function GetCommits() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printErrors(error);
}
}
GetCommits();

In case of successful execution of ...