Skip to content

oreze/git-smartcommit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Git SmartCommit ๐Ÿค–โœ๏ธ

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.

How It Works

The script leverages a prepare-commit-msg Git hook. Here's the workflow:

  1. You run git commit as usual.
  2. The hook triggers and captures all your staged changes (git diff --staged).
  3. It sends the diff to the Google Gemini API with a prompt asking for a commit message that follows the Conventional Commits standard.
  4. The AI-generated message is received and automatically written to your commit message file.
  5. Your default text editor opens with the pre-filled message.
  6. You can edit, approve, or completely change the message.
  7. Save and close the editor to finalize the commit.

Prerequisites

Before you begin, make sure you have the following:

  1. Google Gemini API Key: You need an API key to use the service. You can get one for free from Google AI Studio.

  2. 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

๐Ÿš€ Installation

You can install this hook for a single repository or globally for all your projects.

Step 1: Store Your Gemini API Key

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.)

Step 2: Set Up the Hook

Option A: Install for a Single Repository (Local)

  1. Navigate to your repository's hooks directory:

    cd /path/to/your/repo/.git/hooks
  2. Download the prepare-commit-msg script using curl:

    curl -o prepare-commit-msg https://raw.githubusercontent.com/oreze/git-smartcommit/main/hooks/prepare-commit-msg
  3. Make the script executable:

    chmod +x prepare-commit-msg

Option B: Install for All Repositories (Global)

  1. Create a central directory for your global Git hooks (if you don't have one):

    mkdir -p ~/.git_hooks
  2. 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
  3. Make the script executable:

    chmod +x ~/.git_hooks/prepare-commit-msg
  4. Tell Git to use this directory for hooks in all your repositories:

    git config --global core.hooksPath ~/.git_hooks

Usage

Once installed, just follow your normal Git workflow!

  1. Stage your changes:

    git add .
  2. 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!

Customization

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.

Troubleshooting

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.geminiapikey returns nothing).
  • jq is not installed on your system.

About

Generate your commit messages with AI!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published