AI-powered git commits using Claude Code CLI
A zsh function that analyzes your staged and unstaged changes, then creates logical, atomic commits following the Conventional Commits specification.
When you run smart-commit, Claude will:
- Analyze your changes - Runs
git statusandgit diffto understand what you've modified - Plan the commits - Groups related changes into logical, atomic commits
- Create commits - Stages files with
git addand commits with proper conventional commit messages - Verify - Checks
git statusafter each commit to confirm success
The function uses Claude Code's permission system to restrict what the AI can do:
Allowed:
git status- View repository stategit diff- See changesgit add- Stage filesgit commit- Create commitsRead- Read file contents for context
Blocked (for safety):
git push- Won't push to remotegit reset/git revert- Won't undo commitsgit rebase/git merge- Won't modify historygit checkout/git branch- Won't switch branchesrm/mv- Won't delete or move filesWrite/Edit- Won't modify your code
-
Claude Code CLI - Install from github.com/anthropics/claude-code
npm install -g @anthropic-ai/claude-code
-
Git - Ensure git is installed and configured
-
Open your zsh configuration file:
# macOS / Linux nano ~/.zshrc # or use your preferred editor code ~/.zshrc
-
Add the
smart-commitfunction (copy fromsmart-commit.zsh) -
Reload your shell configuration:
source ~/.zshrc
Navigate to any git repository with changes and run:
smart-commitClaude will analyze all changes and create appropriate commits.
Provide additional context to help Claude understand your changes:
smart-commit "refactoring the authentication flow"smart-commit "bug fix for issue #123"smart-commit "adding new user dashboard feature"$ git status
On branch feature/auth
Changes not staged for commit:
modified: src/auth/login.ts
modified: src/auth/logout.ts
modified: src/utils/validation.ts
modified: README.md
$ smart-commit "updated authentication"
# Claude will:
# 1. Examine the diffs
# 2. Announce its commit strategy
# 3. Create commits like:
# - feat(auth): add session expiry handling
# - refactor(utils): extract email validation
# - docs(readme): update auth setup instructions
The function enforces these commit types:
| Type | Description |
|---|---|
feat |
New feature |
fix |
Bug fix |
docs |
Documentation changes |
style |
Code style (formatting, semicolons, etc.) |
refactor |
Code refactoring (no feature/fix) |
perf |
Performance improvements |
test |
Adding or updating tests |
build |
Build system or dependencies |
ci |
CI/CD configuration |
chore |
Maintenance tasks |
Format: <type>(<scope>): <description>
Examples:
feat(api): add user profile endpointfix(auth): resolve token refresh race conditiondocs(contributing): add PR guidelines
The function uses haiku (fast and cheap) by default. For more complex analysis, change to sonnet:
--model="sonnet"Modify the --append-system-prompt section to change commit style, add team conventions, or adjust the workflow.
-
Review before pushing - The function doesn't push, so always review commits with
git logbefore pushing -
Use context wisely - Providing context helps Claude make better grouping decisions
-
Works best with related changes - If you have many unrelated changes, consider committing in batches
-
Check the plan - Claude announces its strategy before committing - interrupt if needed
MIT - Feel free to modify and share!