Counting GitHub Commits
Retrieve accurate commit counts from GitHub repositories.
By EMEPublished: February 20, 2025
githubgitcommitsapistatistics
A Simple Analogy
Getting commit counts is like counting items on a conveyor belt. Stop at the end to know the total, rather than counting each item individually.
Why Count Commits?
- Statistics: Track project activity
- Analytics: Measure contributor productivity
- Reporting: Show development metrics
- Auditing: Document project history
- Attribution: Credit team contributions
GitHub API Approach
using Octokit;
var client = new GitHubClient(new ProductHeaderValue("MyApp"));
var credentials = new Credentials("github_token");
client.Credentials = credentials;
// Get commit count efficiently
var commits = await client.Repository.Commits.GetAll(
"owner",
"repo",
new CommitRequest { Since = new DateTime(2024, 1, 1) }
);
var totalCommits = commits.Count;
Console.WriteLine($"Total commits: {totalCommits}");
REST API Direct
# Get commits with pagination
curl -H "Authorization: token YOUR_TOKEN" \
"https://api.github.com/repos/OWNER/REPO/commits?per_page=1&page=999999"
# Check Link header for page count
# total_count = per_page × page_count
# Or use GraphQL for efficiency
curl -X POST -H "Authorization: bearer TOKEN" \
-d '{"query":"{ repository(owner:\"OWNER\", name:\"REPO\") { object(expression:\"HEAD\") { ... on Commit { history { totalCount } } } } }"}' \
https://api.github.com/graphql
GraphQL Query
query {
repository(owner: "owner", name: "repo") {
defaultBranchRef {
target {
... on Commit {
history {
totalCount
}
}
}
}
}
}
Best Practices
- Pagination: Use per_page=1 for large repos
- Caching: Cache results, refresh periodically
- Filtering: Filter by date range to narrow scope
- GraphQL: More efficient than REST API
- Rate limits: Use authenticated requests (higher limits)
Related Concepts
- GitHub Statistics API
- Git log analysis
- Repository metrics
- Contributor analytics
Summary
Count commits efficiently using GitHub's REST or GraphQL APIs. Use pagination strategies and caching to minimize API calls.