glossary

Pre-commit Hooks

Git hooks that run automatically before a commit is recorded, allowing teams to enforce code quality checks, linting, or message format validation without manual steps.

Git hooks are shell scripts stored in the .git/hooks directory that Git executes at defined lifecycle points. The pre-commit hook runs after git commit is invoked but before the commit object is written. If the hook exits with a non-zero status, the commit is aborted and the developer must fix the reported issue before trying again. Pre-commit hooks are commonly used to run linters (ESLint, Ruff, golangci-lint), formatters (Prettier, Black, gofmt), type checkers, and test suites on staged files. They can also validate commit message format, check for secrets, or enforce file size limits. Popular frameworks for managing hook installation include pre-commit (Python), Husky (Node.js), and lefthook (Go). One challenge with hooks and AI-generated commit messages is that the hook may reject a message that does not fully conform to the project's specific conventional commit format. A retry loop that regenerates the message with the hook's error output as context can resolve this automatically.

#!/bin/sh npx eslint --max-warnings=0 $(git diff --cached --name-only -- '*.js')
#!/bin/sh gofmt -l $(git diff --cached --name-only -- '*.go') | grep . && exit 1 || exit 0
#!/bin/sh python -m pytest tests/unit/ -q --no-header
# commit-msg hook if ! grep -qE '^(feat|fix|chore|docs|refactor|test|perf|style|ci)(\(.+\))?: .{1,72}$' "$1"; then exit 1; fi

git-agent installs a pre-commit hook via git-agent init that validates each generated message. When a hook rejects a message, git-agent automatically retries message generation up to 3 times, passing the hook's error output back to the LLM as context to produce a conforming message.

brew install gitagenthq/tap/git-agent
Does git-agent bypass my existing pre-commit hooks?
No. git-agent runs git commit normally, which triggers all installed hooks. It does not use --no-verify. If a hook fails, git-agent retries with updated message content.
How many retry attempts does git-agent make when a hook rejects the message?
git-agent retries message generation up to 3 times per commit. After 3 failed attempts it re-plans the commit groups (up to 2 re-plan cycles) and tries again from the planning stage.
Can I use git-agent with Husky or pre-commit framework?
Yes. git-agent is compatible with any hook management system that writes standard Git hooks to .git/hooks. Its retry loop works regardless of how the hook was installed.