How can you display the number of commits made by each contributor to a repository?
Posted by HenryPk
Last Updated: June 19, 2024
You can display the number of commits made by each contributor to a Git repository using a few different methods. Here are some common approaches:
Using Git Command Line
1. Basic Git Log Command: You can use the following command in the terminal to get the list of commits along with authors' names and the count of their commits:
git shortlog -s -n
- -s: Show only the number of commits per author. - -n: Sort output according to the number of commits. 2. Counting Commits by Author: If you want a more detailed view, you can use:
git log --pretty=format:'%an' | sort | uniq -c | sort -nr
- --pretty=format:'%an': Formats the output to show only the author's name. - sort: Sorts the output. - uniq -c: Counts the occurrences of each author's name. - sort -nr: Sorts the result numerically in reverse order, showing the authors with the most commits at the top.
Using GitHub API
If the repository is hosted on GitHub, you can also use the GitHub API to get this information programmatically. Here's a simple example using curl: 1. Get a list of contributors:
curl -s https://api.github.com/repos/<username>/<repository>/contributors
Replace <username> and <repository> with the appropriate repository details. This will give you a JSON response containing a list of contributors along with the number of commits each has made.
Using GitHub Web Interface
1. Go to the repository on GitHub. 2. Click on the "Insights" tab. 3. Select "Contributors" from the menu. This page displays a graph showing the number of commits made by each contributor over time, along with the total number of commits.
Summary
These methods allow you to view the number of commits made by each contributor in a Git repository. The command line is often the quickest way for local repositories, while the GitHub API and web interface provide more user-friendly options for online repositories.