Flutter conventional commits—with git hooks and Lefthook 👑
Conventional Commits, Git Hooks, and Lefthook, a trifecta that can transform, maintaining code quality and consistency in your development.

In the ever-evolving world of Flutter app development, maintaining code quality and consistency is paramount. Enter Conventional Commits, Git Hooks, and Lefthook, a trifecta that can transform your development process. Join in this blog post as we explore how these powerful tools can help you standardize your commit messages, automate code quality checks, and supercharge your Flutter projects. Say goodbye to messy commits and hello to a more organized and efficient development experience!”.
Step 1: Installing Lefthook
To get started with Lefthook and enhance your Git workflow, follow simple installation instructions:
Step 2: Check commit message
Now that you have Lefthook installed and ready to go, let's set up a commit message file written in Dart to ensure consistency in your commit messages. This is especially useful when using Conventional Commits.
- In your Flutter project's root directory, create a new file for your Dart commit message script. You can name it something like
commit_message.dart
. - Open the
commit_message.dart
file in your preferred code editor. - Write your Dart script to generate commit messages. You can customize this script to suit your project's needs and coding conventions. For example:
import 'dart:io';
void main() {
final commitMessageFile = File('.git/COMMIT_EDITMSG');
if (!commitMessageFile.existsSync()) {
print('Commit message file does not exist.');
exit(1);
}
final commitMessage = commitMessageFile.readAsStringSync();
if (!isValidCommitMessage(commitMessage)) {
print('👎 Invalid commit message format.');
print('Commit message should follow the Conventional Commits format.');
exit(1);
}
print('👍 Valid commit message!');
exit(0);
}
bool isValidCommitMessage(String commitMessage) {
final RegExp pattern = RegExp(r'^(feat|fix|docs|chore)(\(.+\))?: .+');
return pattern.hasMatch(commitMessage);
}
Validation commit message code
Step 3: Adding the Commit Message Script to Your Lefthook Configuration
Now that you have your Dart commit message script ready, it's time to integrate it into your Lefthook configuration so that Lefthook can automatically generate commit messages using your script.
- In your project's root directory, locate or create the
lefthook
.yml file. If it doesn't exist, you can initialize Lefthook by runningnpx lefthook install
in your project directory. - Open this file using your preferred code editor.
- In the
pre-commit
section of yourlefthook.yml
file, add a call to your Dart commit message script. For example:
commit-msg:
commands:
- name: Dart Commit Message Script
validate:
run: flutter pub run ./commit_message.dart
Save the lefthook.yml
file.
Step 4: Test it
If everything was done correctly, you should see the following message during the correct commit message:

And if the commit message does not follow the conventional commit standard, you should see the following message:

Summary
With this setup, Lefthook will execute your Dart commit message script before each commit, allowing you to generate and validate commit messages automatically.
Source code
In case of any problems, take a look at the sample repository with the above configuration:
Thanks for reading ♥️♥️