AI-powered Git commit message generator for Emacs
This README was generated with assistance from Claude Sonnet 4
🤖 Generates commit messages based on staged changes
💭 Optional rationale input for complex changes
🔄 Streaming support for real-time generation
🎯 Smart filtering of lock files and generated code
gptel-commit is available on MELPA. After setting up MELPA, you can install it with:
(use-package gptel-commit
:ensure t
:after (gptel magit)
:custom
(gptel-commit-stream t))
Or with M-x package-install RET gptel-commit RET
.
Install and configure gptel
Ensure you have Git installed and accessible from Emacs
Main functions available:
M-x gptel-commit
- Generate commit message directly from staged changes
M-x gptel-commit-rationale
- Generate with optional rationale/context prompt
When using Magit, these functions work in commit message buffers. You can bind them to convenient keys in your configuration.
(with-eval-after-load 'magit
(define-key git-commit-mode-map (kbd "C-c g") #'gptel-commit)
(define-key git-commit-mode-map (kbd "C-c G") #'gptel-commit-rationale))
When using gptel-commit-rationale
, you’ll be prompted to explain why you’re making these changes. This helps the AI generate more meaningful commit messages with proper context.
;; Enable/disable streaming (default: t)
(setq gptel-commit-stream t)
;; Use a specific backend for commit messages
;; (e.g., a cheaper model via OpenRouter)
;; See https://github.com/karthink/gptel?tab=readme-ov-file#setup for more backend options
(setq gptel-commit-backend (gptel-make-gh-copilot "Copilot"))
;; Exclude files from diff analysis (default patterns shown)
(setq gptel-commit-diff-excludes
'("pnpm-lock.yaml"
"*.lock"
"ent/**/*.go"))
;; Customize the prompt used for generating commit messages
;; Example using Zed editor's commit message prompt style:
;; https://github.com/zed-industries/zed/blob/main/crates/git_ui/src/commit_message_prompt.txt
(setq gptel-commit-prompt
"You are an expert at writing Git commits. Your job is to write a short clear commit message that summarizes the changes.
If you can accurately express the change in just the subject line, don't include anything in the message body. Only use the body when it is providing *useful* information.
Don't repeat information from the subject line in the message body.
Only return the commit message in your response. Do not include any additional meta-commentary about the task. Do not include the raw diff output in the commit message.
Follow good Git style:
- Separate the subject from the body with a blank line
- Try to limit the subject line to 50 characters
- Capitalize the subject line
- Do not end the subject line with any punctuation
- Use the imperative mood in the subject line
- Wrap the body at 72 characters
- Keep the body short and concise (omit it entirely if not useful)")
gptel-commit-after-insert-hook
: run after commit message is inserted.
Analyzes staged changes using git diff --cached
Filters out excluded files (lock files, generated code, etc.)
Sends the diff to your configured LLM with a specialized prompt
Formats the response according to conventional Git commit message format
Inserts the message in your commit buffer
Keep your staged changes focused and logical
Use the rationale feature for complex changes
The AI works best with clear, well-structured code changes
Review and edit the generated message as needed
Contributions are welcome! Please feel free to submit issues and pull requests.
GPL-3.0