Isaac.

devops

Counting GitHub Commits

Retrieve accurate commit counts from GitHub repositories.

By Emem IsaacApril 2, 20232 min read
#github#git#commits#api#statistics
Share:

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

  1. Pagination: Use per_page=1 for large repos
  2. Caching: Cache results, refresh periodically
  3. Filtering: Filter by date range to narrow scope
  4. GraphQL: More efficient than REST API
  5. 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.

Share:

Written by Emem Isaac

Expert Software Engineer with 15+ years of experience building scalable enterprise applications. Specialized in ASP.NET Core, Azure, Docker, and modern web development. Passionate about sharing knowledge and helping developers grow.

Ready to Build Something Amazing?

Let's discuss your project and explore how my expertise can help you achieve your goals. Free consultation available.

💼 Trusted by 50+ companies worldwide | ⚡ Average response time: 24 hours