Today, I needed to produce a CSV containing all commits made to a git repo 2 years ago. Did I say I hate audits? Luckily, it wasn’t that hard.
git log --after='2016-12-31' --before='2018-1-1' --pretty=format: '%h',%an,%ai,'%s' > log.csv
To give a quick breakdown:
- git log – this is the command to output the commit log.
- –after=’2016-12-31′ – this limits the results to commits after the date.
- –before=’2018-1-1′ – this limits the results to commits before the date.
- pretty=format:’%h’,%an,%ai,’%s’ – this is outputting the log in the specified format:
- ‘%h’ – hash with surrounded by single quotes
- %an – author name
- %ai – ISO 8601 formatted date of commit
- ‘%s’ – commit message surrounded by single quote.
- > log.csv – output the log to a csv file named log.csv
I surround some values with single quotes to prevent Excel from interpreting the values as numbers or other value that loses the desired format. I had to look through the pretty format docs to find the placeholders to get the output I wanted.
It took a little digging through git docs to get here: https://git-scm.com/docs/git-log and https://git-scm.com/docs/pretty-formats. If I would have been smart and just searched for it I would have landed on this stack: https://stackoverflow.com/questions/10418056/how-do-i-generate-a-git-commit-log-for-the-last-month-and-export-it-as-csv.