Git Commit
Sensitive File Guard
Before staging, scan git status for sensitive patterns:
.env*, *.pem, *.key, *.p12, *.pfx
*.tfstate, *.tfvars (with real values)
credentials.json, serviceAccountKey.json, *secret*
If a match is found:
- Append the missing pattern to
.gitignore and stage .gitignore.
- If the file is already tracked, warn and suggest
git rm --cached <file>.
- Never proceed with committing a sensitive file.
Smart Commit Workflow
1. Review
git status
git diff
git diff --cached
git log --oneline -5
2. Run Sensitive File Guard
Scan the output of git status against the patterns above.
3. Auto-stage tracked changes
git add -u
Stage tracked modifications and deletions only. Do not stage untracked files automatically — review them first and stage by name if appropriate.
4. Analyse for multi-concern splitting
Group changed files by directory or feature area. If changes span unrelated concerns (e.g. a bug fix and a new feature), split into separate commits automatically.
5. Generate commit message
- Infer type from the change:
feat, fix, chore, docs, refactor, test, style.
- Infer scope from the primary directory or feature area (e.g.
components, terraform, skills).
- Subject line: imperative voice, under 72 characters.
- Body (optional): explain "why", not "what".
6. Commit via heredoc
git commit -m "$(cat <<'EOF'
type(scope): subject line
Optional body.
Co-Authored-By: Claude <model> <noreply@anthropic.com>
EOF
)"
Replace <model> with the actual model name (e.g. Opus 4.6, Sonnet 4.6).
7. Handle pre-commit hook failure
Fix the issue, re-stage, and create a new commit. Never use --no-verify. Never amend — the failed commit does not exist.
Worktree Workflow
When working in a git worktree (.claude/worktrees/<name>/):
- Smart commit all changes in the worktree using the workflow above.
- Switch to
main in the primary working directory.
- Squash-merge the worktree branch:
git merge --squash <worktree-branch>
- Commit the squashed result with a single well-formed message.
- Verify:
git log --oneline -5 && git status.
- Remove the worktree:
git worktree remove .claude/worktrees/<name>.
Rules: commit before merging, always merge into main, never delete the branch before merge is confirmed, use --squash to keep history linear.
Rules
- Proceed without confirmation — do not ask the user to approve the commit message or confirm before committing. Stage, generate the message, and commit in one flow.
- Never commit sensitive files (run the guard first).
- Never amend a published commit — create a new one.
- Never force-push
main.
- Never use
--no-verify or --no-gpg-sign.
- Prefer named files over
git add -A to avoid staging secrets or noise.
- Formatting and linting run automatically via hooks.
Quick Reference
- Run Sensitive File Guard before every commit.
- Auto-stage tracked changes with
git add -u, review untracked files individually.
- Split unrelated concerns into separate commits.
- Conventional Commits with auto-detected type and scope.
- Commit via heredoc with
Co-Authored-By trailer.
- Worktree: commit, squash-merge to main, verify, remove.
1---2name: git-commit3description: Use when committing changes, staging files, or finishing work in a git worktree. Covers smart commit, multi-concern splitting, sensitive-file guarding, and worktree merge.4---56# Git Commit78## Sensitive File Guard910Before staging, scan `git status` for sensitive patterns:1112- `.env*`, `*.pem`, `*.key`, `*.p12`, `*.pfx`13- `*.tfstate`, `*.tfvars` (with real values)14- `credentials.json`, `serviceAccountKey.json`, `*secret*`1516If a match is found:17181. Append the missing pattern to `.gitignore` and stage `.gitignore`.192. If the file is already tracked, warn and suggest `git rm --cached <file>`.203. Never proceed with committing a sensitive file.2122## Smart Commit Workflow2324### 1. Review2526```bash27git status28git diff29git diff --cached30git log --oneline -531```3233### 2. Run Sensitive File Guard3435Scan the output of `git status` against the patterns above.3637### 3. Auto-stage tracked changes3839```bash40git add -u41```4243Stage tracked modifications and deletions only. Do not stage untracked files automatically — review them first and stage by name if appropriate.4445### 4. Analyse for multi-concern splitting4647Group changed files by directory or feature area. If changes span unrelated concerns (e.g. a bug fix and a new feature), split into separate commits automatically.4849### 5. Generate commit message5051- Infer **type** from the change: `feat`, `fix`, `chore`, `docs`, `refactor`, `test`, `style`.52- Infer **scope** from the primary directory or feature area (e.g. `components`, `terraform`, `skills`).53- Subject line: imperative voice, under 72 characters.54- Body (optional): explain "why", not "what".5556### 6. Commit via heredoc5758```bash59git commit -m "$(cat <<'EOF'60type(scope): subject line6162Optional body.6364Co-Authored-By: Claude <model> <noreply@anthropic.com>65EOF66)"67```6869Replace `<model>` with the actual model name (e.g. `Opus 4.6`, `Sonnet 4.6`).7071### 7. Handle pre-commit hook failure7273Fix the issue, re-stage, and create a **new** commit. Never use `--no-verify`. Never amend — the failed commit does not exist.7475## Worktree Workflow7677When working in a git worktree (`.claude/worktrees/<name>/`):78791. **Smart commit** all changes in the worktree using the workflow above.802. **Switch to `main`** in the primary working directory.813. **Squash-merge** the worktree branch:82 ```bash83 git merge --squash <worktree-branch>84 ```854. **Commit** the squashed result with a single well-formed message.865. **Verify**: `git log --oneline -5 && git status`.876. **Remove** the worktree: `git worktree remove .claude/worktrees/<name>`.8889Rules: commit before merging, always merge into `main`, never delete the branch before merge is confirmed, use `--squash` to keep history linear.9091## Rules9293- **Proceed without confirmation** — do not ask the user to approve the commit message or confirm before committing. Stage, generate the message, and commit in one flow.94- Never commit sensitive files (run the guard first).95- Never amend a published commit — create a new one.96- Never force-push `main`.97- Never use `--no-verify` or `--no-gpg-sign`.98- Prefer named files over `git add -A` to avoid staging secrets or noise.99- Formatting and linting run automatically via hooks.100101## Quick Reference102103- Run Sensitive File Guard before every commit.104- Auto-stage tracked changes with `git add -u`, review untracked files individually.105- Split unrelated concerns into separate commits.106- Conventional Commits with auto-detected type and scope.107- Commit via heredoc with `Co-Authored-By` trailer.108- Worktree: commit, squash-merge to main, verify, remove.