To determine the number of days with commits in a specific month for a Git repository, various command-line approaches can be used depending on the environment.
Using Git Bash for Unix-like Systems
Navigate to the local Git repository directory in Git Bash. Execute the following command to count commit days for a given month, such as February 2024:
git log --pretty=format:'%ad' --date=format:'%Y-%m-%d' | grep '^2024-02' | sort -u | wc -l
This pipeline extracts commit dates in YYYY-MM-DD format, filters lines starting with the specified month, sorts unique dates, and outputs the count.
Using Windows PowerShell
In PowerShell, a similar result can be achieved with:
git log --pretty=format:'%ad' --date=format:'%Y-%m-%d' | Select-String -Pattern '2024-02' | Get-Unique | Measure-Object | Select-Object -ExpandProperty Count
Here, the command selects lines matching the month pattern, removes duplicates, and calculates the total unique antries.
Including All Branches
To incorporate commits from all branches, add the --all flag. For example, in PowerShell:
git log --all --pretty=format:'%ad' --date=format:'%Y-%m-%d' | Select-String -Pattern '2024-02' | Get-Unique | Measure-Object | Select-Object -ExpandProperty Count
Replace '2024-02' with the desired year and month in YYYY-MM format. The components function as follows:
--all: Includes commits from every branch.--pretty=format:'%ad': Outputs only the author date.--date=format:'%Y-%m-%d': Formats the date consistently.Select-String -Pattern '2024-02': Filters commits too the specified month.Get-Unique: Groups by distinct dates.Measure-ObjectwithSelect-Object: Counts the unique dates, representing days with commits.