A Python script that automatically generates commit messages based on git changes analysis. This tool helps save time writing commit messages and ensures consistency by supporting the Conventional Commits specification.
- Change Analysis: Automatically analyzes git changes (added, modified, deleted files)
- Commit Message Suggestions: Generates appropriate commit messages based on file changes
- Conventional Commits Support: Follows Conventional Commits specification for consistent commit history
- Interactive Mode: Allows customization of suggested commit messages
- Smart Type Detection: Automatically detects commit type (feat, fix, docs, etc.) based on file patterns and changes
Writing good commit messages can be time-consuming and maintaining consistency across a project is challenging. This script automates the process by:
- Analyzing git changes to understand what was modified
- Suggesting appropriate commit messages following Conventional Commits format
- Reducing the time spent on writing commit messages
- Ensuring consistency in commit message style across the project
- Python 3.6 or higher
- Git installed and configured
- The script must be run in a git repository
No external dependencies required. The script uses only Python standard library.
-
Navigate to your git repository:
cd /path/to/your/git/repository -
Make some changes to your files (add, modify, or delete files)
-
Stage your changes (optional):
git add . -
Run the script:
python script.py
-
Follow the interactive prompts:
- Review the suggested commit message
- Choose to use it, edit it, or create a custom message
- Confirm to create the commit
- Change Detection: The script analyzes git status to identify staged, unstaged, and untracked files
- File Analysis: Examines file changes using
git diffto understand what was added, removed, or modified - Pattern Matching: Detects commit type based on:
- File patterns (e.g.,
.mdfiles →docs, test files →test) - Change patterns (e.g., bug fixes →
fix, new features →feat) - Code analysis (keywords in diffs)
- File patterns (e.g.,
- Message Generation: Creates a commit message following Conventional Commits format:
<type>(<scope>): <description> <body>
The script supports the following Conventional Commits types:
feat: A new featurefix: A bug fixdocs: Documentation only changesstyle: Changes that do not affect the meaning of the coderefactor: A code change that neither fixes a bug nor adds a featureperf: A code change that improves performancetest: Adding missing tests or correcting existing testsbuild: Changes that affect the build system or external dependenciesci: Changes to CI configuration files and scriptschore: Other changes that do not modify src or test filesrevert: Reverts a previous commit
# After adding a new file: feature.py
$ python script.py
Suggested commit message:
------------------------------------------------------------
feat: add feature
Modified files: feature.py
------------------------------------------------------------# After modifying buggy_code.py to fix an issue
$ python script.py
Suggested commit message:
------------------------------------------------------------
fix(buggy_code): fix issue in buggy_code
Modified files: buggy_code.py
------------------------------------------------------------# After updating README.md
$ python script.py
Suggested commit message:
------------------------------------------------------------
docs: update README
Modified files: README.md
------------------------------------------------------------When running the script, you'll be presented with options:
- Use suggested message: Accept the generated message as-is
- Edit commit type: Change the commit type (feat, fix, etc.)
- Edit description: Modify the description part of the message
- Enter custom message: Write your own commit message
- Cancel: Exit without creating a commit
- The script analyzes up to 10 files for performance reasons
- It works with both staged and unstaged changes
- If you choose not to auto-commit, you can manually use the suggested message
- The script must be run from within a git repository
- Support for breaking changes notation (
!) - Integration with git hooks for automatic message generation
- Support for multi-line commit bodies with detailed change descriptions
- Configuration file for custom commit type patterns
- Support for different commit message templates
- Integration with issue tracking systems (GitHub, GitLab, etc.)
Created as part of the Daily Python Scripts collection.