DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Marc Pires
Marc Pires

Posted on • Originally published at Medium on

Automatic CHANGELOG with git

Automatic CHANGELOG with git

This is a simple tip of how you can generate automatic changelogs for your projects using git.

Know your hooks

For Git novices that never dared to tap into the .git folder, there resides the hooks folder. Inside this folder, you'll find a series of scripts can be activated and will run on git actions, let's take a look.

Content of .git/hooks subfolder

As you see for the image above, there are bunch of hooks we can activate. Here some:

pre-push : This will always run, as its name implies, before a push. You can use this hook for example, to run all your tests before you send code to a remote.

pre-commit : Runs before a commit

The CHANGELOG hook

Know, let's go straight to the point.

First create a post-commit file in the .git/hooks folder. The contents of my post-commit file is as follows.

The script is very simple, I do a git log with 3 parameters:

- -no-pager : Do not pipe Git output into a pager (less)

- -no-merges: Does not print commits with more than one parent (same as max-parents=1)

- -format : Pretty print the commit content in a specific format, possible values are β€˜oneline’, β€˜short’, β€˜medium’, β€˜full’, β€˜fuller’, β€˜email’, β€˜raw’, β€˜format:’ and β€˜tformat:’

The log output is then redirected do the CHANGELOG.md file on the project root folder.

After saving the file, just do a chmod +x .git/hooks/post-commit and that's it. Now, always after the commit, your CHANGELOG.md will be updated.

I hope you find it useful and does not forget to comment. 'till next tip.

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.