Never write a commit message from scratch again.
Smart-Commit is a simple yet powerful Git hook that uses Google's Gemini AI to automatically generate descriptive and conventional commit messages for you. It analyzes your staged changes, generates a message, and lets you review and edit it before committing.
The script leverages a prepare-commit-msg Git hook. Here's the workflow:
- You run
git commitas usual. - The hook triggers and captures all your staged changes (
git diff --staged). - It sends the diff to the Google Gemini API with a prompt asking for a commit message that follows the Conventional Commits standard.
- The AI-generated message is received and automatically written to your commit message file.
- Your default text editor opens with the pre-filled message.
- You can edit, approve, or completely change the message.
- Save and close the editor to finalize the commit.
Before you begin, make sure you have the following:
-
Google Gemini API Key: You need an API key to use the service. You can get one for free from Google AI Studio.
-
jq: A command-line JSON processor. The script uses it to safely build the API request and parse the response.# On macOS (using Homebrew) brew install jq # On Debian/Ubuntu sudo apt-get install jq # On Fedora/CentOS/RHEL sudo dnf install jq
You can install this hook for a single repository or globally for all your projects.
For security, store your API key in your global Git configuration instead of hardcoding it.
git config --global google.geminiapikey YOUR_GEMINI_API_KEY(Replace YOUR_GEMINI_API_KEY with the key you got from Google AI Studio.)
-
Navigate to your repository's hooks directory:
cd /path/to/your/repo/.git/hooks -
Download the
prepare-commit-msgscript usingcurl:curl -o prepare-commit-msg https://raw.githubusercontent.com/oreze/git-smartcommit/main/hooks/prepare-commit-msg
-
Make the script executable:
chmod +x prepare-commit-msg
-
Create a central directory for your global Git hooks (if you don't have one):
mkdir -p ~/.git_hooks -
Download the script into that directory:
curl -o ~/.git_hooks/prepare-commit-msg https://raw.githubusercontent.com/oreze/git-smartcommit/main/hooks/prepare-commit-msg -
Make the script executable:
chmod +x ~/.git_hooks/prepare-commit-msg -
Tell Git to use this directory for hooks in all your repositories:
git config --global core.hooksPath ~/.git_hooks
Once installed, just follow your normal Git workflow!
-
Stage your changes:
git add . -
Run the commit command:
git commit
Your text editor will pop up with a commit message generated by Gemini. Review it, make any desired changes, save, and close. Done!
Feel free to modify the script to fit your needs. The easiest thing to change is the AI prompt. Open the prepare-commit-msg file and edit the PROMPT variable:
# ...
# You can customize this prompt to get the style you want
PROMPT="Based on the following git diff, please generate a very short and witty commit message. The diff is:
$STAGED_DIFF"
# ...You can also change the Gemini model used (e.g., to gemini-2.5-flash for faster, cheaper generation) by updating the URL in the curl command inside the script.
If you see an error like Error: Could not extract message from Gemini API response, the script will print the full API response to your console. This is useful for debugging. The most common issues are:
- An invalid or missing API key (
git config --get google.geminiapikeyreturns nothing). jqis not installed on your system.