Automatic changelog with Git

If you use Git (why not with SourceTree as in this article ), you may want to have a changelog that is created automatically on your application. We had this need recently, I take this opportunity to give you the appropriate code.

Creating the changelog is not very complicated. You’re going to have to play around with Git’s different rendering options to get something clean and satisfying. For example, you may only want to display commit messages that you typed. And not all the messages indicating merges between the different branches. They don’t necessarily need to be on a changelog.

You can use the following line to obtain information in table cells:

git log --no-merges --pretty=format:'<tr><td>%ci</td><td>%an</td><td>%s</td></tr>'

We are on our side on a PHP application. We therefore do a shell_exec of this line to retrieve this piece of array in a variable. All you have to do is finish the table (beginning and end of table, header) and display it.

The values ​​with % (%ci, %an, %s) are the values ​​managed by the pretty format.
In the case of the example above, this will display the date, author and subject of the commit. You will have on this page of the documentation the list of the possible values ​​and to what they correspond to adapt the table to what you wish.

Leave a Reply