Commits Command
Make commits for all necessary changes in the working directory.
Argument Handling
Check $ARGUMENTS for these flags (case-insensitive, hyphens optional):
- "push": Also push to the remote after committing.
- "--nopush" or "--no-push": Before committing, invoke
/push-forbid to disable automatic pushing for the rest of the session.
- "--pushok" or "--push-ok" or "--pushauto" or "--push-auto": Before committing, invoke
/push-auto to enable automatic pushing for the rest of the session.
Execution Strategy (Token Cost Optimization)
Offload the commit workflow to a Haiku subagent so the expensive main session context (the session model — currently Fable 5) only sees a summary — not the full git diff, file-selection reasoning, or commit-message drafting.
Step 0: Pre-check (skip subagent when nothing to do)
Before anything else, run these two checks via Bash:
git status --porcelain
git rev-list --count @{upstream}..HEAD 2>/dev/null || echo "unknown"
Based on the results:
- Nothing to commit + nothing to push → Report "Nothing to commit or push." and stop immediately. Done.
- Nothing to commit + commits ahead +
push argument present → No subagent needed. Push per step 6's rules (check behind-count first; do NOT pull unconditionally). Report and stop. Done.
- Nothing to commit + "unknown" (no upstream) +
push argument present → New branch with no remote tracking. Run git push -u origin $(git branch --show-current). Report and stop. Done.
- Nothing to commit + commits ahead + NO
push argument → Report "Nothing to commit. N commit(s) ahead of remote — use /commits push to push." and stop. Done.
- Something to commit → Continue to Attempt 1 below.
Attempt 1: Haiku subagent
Spawn a Haiku subagent using the Agent tool with model: "haiku". Give it the full Instructions section below as its prompt, plus the current repo path and whether push is requested. The subagent runs in its own isolated context — the main session context (the session model — currently Fable 5) only sees the final report, not the intermediate git diff/status output.
If the subagent reports success: stop here. Done.
If the subagent fails (partial commits, rebase conflict, or any error): run git status and git log --oneline to assess current state, then continue with Attempt 2 for the remainder.
Attempt 2: Direct execution (last resort)
Only if Attempt 1 failed, execute the Instructions below directly in the current session. Handles commit and (if push argument present) push.
Conflict Handling (merge conflict during push)
If git pull --no-rebase fails due to a conflict:
First, confirm the branch was genuinely behind. git rev-list --count HEAD..@{upstream} should be non-zero. If it is 0 the pull should never have run — abort it, just git push, and treat the "conflict" as an artifact of the pull, not a real divergence.
If running as a child agent in a team:
- Do NOT attempt conflict resolution — you lack the full picture
- Abort with
git merge --abort
- Report to the manager immediately with: branch name, that a merge conflict occurred, and any conflict details from the output
- The manager will judge complexity and either resolve it directly or spawn an Opus subagent with full implementation context
If running standalone (no team):
- Run
git merge --abort to restore the pre-merge state
- Run
git fetch && git log --oneline HEAD..origin/$(git branch --show-current) to see incoming commits
- Run
git pull --no-rebase again to see the actual conflicts
- Assess complexity:
- Simple (whitespace, non-overlapping, trivial): resolve directly,
git commit, then git push
- Complex (overlapping logic, multiple files, unclear intent): abort with
git merge --abort, then resolve carefully with full awareness of what was implemented before retrying push
Instructions
- Check current status
- Run
git status to see all modified, staged, and untracked files
- Run
git diff --stat to understand the scope of changes
Check for unwanted files and update .gitignore
Before proceeding, scan the output for files that should never be committed:
Files/directories to exclude:
node_modules/ - Package dependencies
Build outputs: dist/, build/, .next/, out/, *.bundle.js
Log files: *.log, npm-debug.log*, yarn-error.log
Temporary files: *.tmp, *.temp, .cache/, *.swp, *~
OS files: .DS_Store, Thumbs.db, Desktop.ini
IDE files: .idea/, .vscode/, *.sublime-* (unless intentional)
Environment files: .env, .env.local, .env*.local
Test coverage: coverage/, .nyc_output/
Package manager: pnpm-lock.yaml, package-lock.json, yarn.lock (context-dependent)
Secrets/credentials: *.pem, *.key, credentials.json, secrets.*
If unwanted files are found:
Check if .gitignore exists in the project root
If it exists, check if the unwanted patterns are already listed
If patterns are missing, add them to .gitignore
If .gitignore doesn't exist and project has package.json or other project markers, create one with common patterns
After updating .gitignore, the ignored files will no longer appear in git status
If any unwanted files are already staged:
Unstage them with git reset HEAD <file>
If they were previously committed, inform the user they may need to remove them from history
Temporary image/screenshot files (NEVER add to .gitignore):
Image files (*.png, *.jpg, *.gif, *.webp, screenshot SVGs) in the repo root or non-content directories are typically temporary — generated by headless browsers or shared during conversation. Do NOT add glob patterns like *.png to .gitignore. Instead:
If the files appear no longer needed (old screenshots, leftover from previous work): delete them with rm
If the files appear still useful (referenced in the current conversation): move them to the log directory: LOGDIR=$(node $HOME/.claude/scripts/get-logdir.js) && mkdir -p "$LOGDIR" && mv <file> "$LOGDIR/"
Never add image glob patterns to .gitignore — it confuses users about what the pattern is for and may accidentally exclude intentional image assets
- Filter and select files to commit
- Review remaining files after .gitignore filtering
- Only stage files that contain intentional, meaningful changes
- Skip auto-generated files (timestamps, caches, lock files unless relevant)
- If unsure about a file, ask the user
- Analyze and group changes
- If changes are small and related: make a single commit
- If changes span multiple unrelated concerns: separate into logical commits
- Examples of good separation:
- Documentation changes vs code changes
- Feature additions vs bug fixes
- Refactoring vs new functionality
- Config changes vs source changes
- Create commits
Write clear, concise commit messages
Use conventional commit style when appropriate (feat:, fix:, docs:, refactor:, etc.)
Add Co-Authored-By: Claude <noreply@anthropic.com> if Claude contributed significantly
Use HEREDOC format for commit messages:
git commit -m "$(cat <<'EOF'
Commit message here
Co-Authored-By: Claude <noreply@anthropic.com>
EOF
)"
- Push (only if
push argument is present)
Check commits ahead: git rev-list --count @{upstream}..HEAD 2>/dev/null || echo "unknown"
If 0 commits ahead: nothing to push, skip to Verify
If "unknown" (no upstream / new branch): run git push -u origin $(git branch --show-current), skip to Verify
Check whether the branch is actually behind before pulling anything:
git fetch
git rev-list --count HEAD..@{upstream} # 0 = not behind
If 0 (not behind): run git push directly. Do NOT pull. A pull here is a no-op at best; at worst --rebase replays every local commit onto the remote tip, which flattens any merge commit on the branch and re-applies its contents as fresh work — that manufactures conflicts (typically in lockfiles) on a branch that had nothing to reconcile.
If non-zero (genuinely behind): run git pull --no-rebase (regular merge — the default strategy per the user's global CLAUDE.md; --rebase would rewrite local history) then git push
If the pull fails with a conflict, follow the Conflict Handling section above
- Verify
- Run
git status after committing to confirm working tree is clean
- Run
git log --oneline -n <number of commits> to show what was committed
Important Notes
- Never use
git add . blindly - be selective about what to stage
- If unsure whether a file should be committed, ask the user
- Prefer smaller, focused commits over large monolithic commits
- Always verify the commit was successful before moving on
- Never use
git commit --amend without explicit user permission - Always create new commits by default. Amending commits can be confusing and may cause issues if the original commit was already shared. If you need to fix a previous commit, ask the user first.
- Proactively update .gitignore - If you see files that should be ignored (node_modules, .env, logs, etc.), update .gitignore before committing. This prevents accidental commits of sensitive or unnecessary files in the future. Exception: never add image glob patterns (*.png, etc.) — delete or move those instead (see above).
1---2name: commits3description: Commit necessary changes with appropriate separation. Use when: (1) User says 'commit', 'commits', or 'save changes', (2) Claude has made changes that need committing, (3) User wants commits with proper grouping and conventional messages. Handles .gitignore updates, file selection, logical grouping, clean commit messages.4---56# Commits Command78Make commits for all necessary changes in the working directory.910## Argument Handling1112Check `$ARGUMENTS` for these flags (case-insensitive, hyphens optional):1314- **"push"**: Also push to the remote after committing.15- **"--nopush" or "--no-push"**: Before committing, invoke `/push-forbid` to disable automatic pushing for the rest of the session.16- **"--pushok" or "--push-ok" or "--pushauto" or "--push-auto"**: Before committing, invoke `/push-auto` to enable automatic pushing for the rest of the session.1718## Execution Strategy (Token Cost Optimization)1920Offload the commit workflow to a Haiku subagent so the expensive main session context (the session model — currently Fable 5) only sees a summary — not the full git diff, file-selection reasoning, or commit-message drafting.2122### Step 0: Pre-check (skip subagent when nothing to do)2324Before anything else, run these two checks via Bash:2526```bash27git status --porcelain28git rev-list --count @{upstream}..HEAD 2>/dev/null || echo "unknown"29```3031Based on the results:3233- **Nothing to commit + nothing to push** → Report "Nothing to commit or push." and **stop immediately**. Done.34- **Nothing to commit + commits ahead + `push` argument present** → No subagent needed. Push per step 6's rules (check behind-count first; do NOT pull unconditionally). Report and **stop**. Done.35- **Nothing to commit + "unknown" (no upstream) + `push` argument present** → New branch with no remote tracking. Run `git push -u origin $(git branch --show-current)`. Report and **stop**. Done.36- **Nothing to commit + commits ahead + NO `push` argument** → Report "Nothing to commit. N commit(s) ahead of remote — use `/commits push` to push." and **stop**. Done.37- **Something to commit** → Continue to Attempt 1 below.3839### Attempt 1: Haiku subagent4041Spawn a **Haiku subagent** using the Agent tool with `model: "haiku"`. Give it the full Instructions section below as its prompt, plus the current repo path and whether `push` is requested. The subagent runs in its own isolated context — the main session context (the session model — currently Fable 5) only sees the final report, not the intermediate git diff/status output.4243If the subagent reports success: **stop here**. Done.4445If the subagent fails (partial commits, rebase conflict, or any error): run `git status` and `git log --oneline` to assess current state, then continue with Attempt 2 for the remainder.4647### Attempt 2: Direct execution (last resort)4849Only if Attempt 1 failed, execute the Instructions below directly in the current session. Handles commit and (if `push` argument present) push.5051---5253## Conflict Handling (merge conflict during push)5455If `git pull --no-rebase` fails due to a conflict:5657**First, confirm the branch was genuinely behind.** `git rev-list --count HEAD..@{upstream}` should be non-zero. If it is 0 the pull should never have run — abort it, just `git push`, and treat the "conflict" as an artifact of the pull, not a real divergence.5859**If running as a child agent in a team:**60611. Do NOT attempt conflict resolution — you lack the full picture622. Abort with `git merge --abort`633. Report to the manager immediately with: branch name, that a merge conflict occurred, and any conflict details from the output644. The manager will judge complexity and either resolve it directly or spawn an Opus subagent with full implementation context6566**If running standalone (no team):**67681. Run `git merge --abort` to restore the pre-merge state692. Run `git fetch && git log --oneline HEAD..origin/$(git branch --show-current)` to see incoming commits703. Run `git pull --no-rebase` again to see the actual conflicts714. Assess complexity:72- **Simple** (whitespace, non-overlapping, trivial): resolve directly, `git commit`, then `git push`73- **Complex** (overlapping logic, multiple files, unclear intent): abort with `git merge --abort`, then resolve carefully with full awareness of what was implemented before retrying push7475---7677## Instructions78791. **Check current status**8081- Run `git status` to see all modified, staged, and untracked files82- Run `git diff --stat` to understand the scope of changes83842. **Check for unwanted files and update .gitignore**8586 Before proceeding, scan the output for files that should never be committed:8788 **Files/directories to exclude:**8990- `node_modules/` - Package dependencies91- Build outputs: `dist/`, `build/`, `.next/`, `out/`, `*.bundle.js`92- Log files: `*.log`, `npm-debug.log*`, `yarn-error.log`93- Temporary files: `*.tmp`, `*.temp`, `.cache/`, `*.swp`, `*~`94- OS files: `.DS_Store`, `Thumbs.db`, `Desktop.ini`95- IDE files: `.idea/`, `.vscode/`, `*.sublime-*` (unless intentional)96- Environment files: `.env`, `.env.local`, `.env*.local`97- Test coverage: `coverage/`, `.nyc_output/`98- Package manager: `pnpm-lock.yaml`, `package-lock.json`, `yarn.lock` (context-dependent)99- Secrets/credentials: `*.pem`, `*.key`, `credentials.json`, `secrets.*`100101 **If unwanted files are found:**1021031. Check if `.gitignore` exists in the project root1042. If it exists, check if the unwanted patterns are already listed1053. If patterns are missing, add them to `.gitignore`1064. If `.gitignore` doesn't exist and project has `package.json` or other project markers, create one with common patterns1075. After updating `.gitignore`, the ignored files will no longer appear in `git status`108109 **If any unwanted files are already staged:**110111- Unstage them with `git reset HEAD <file>`112- If they were previously committed, inform the user they may need to remove them from history113114 **Temporary image/screenshot files (NEVER add to .gitignore):**115116 Image files (`*.png`, `*.jpg`, `*.gif`, `*.webp`, screenshot SVGs) in the repo root or non-content directories are typically temporary — generated by headless browsers or shared during conversation. Do NOT add glob patterns like `*.png` to `.gitignore`. Instead:117118- If the files appear **no longer needed** (old screenshots, leftover from previous work): **delete them** with `rm`119- If the files appear **still useful** (referenced in the current conversation): **move them** to the log directory: `LOGDIR=$(node $HOME/.claude/scripts/get-logdir.js) && mkdir -p "$LOGDIR" && mv <file> "$LOGDIR/"`120- Never add image glob patterns to `.gitignore` — it confuses users about what the pattern is for and may accidentally exclude intentional image assets1211223. **Filter and select files to commit**123124- Review remaining files after .gitignore filtering125- Only stage files that contain intentional, meaningful changes126- Skip auto-generated files (timestamps, caches, lock files unless relevant)127- If unsure about a file, ask the user1281294. **Analyze and group changes**130131- If changes are small and related: make a single commit132- If changes span multiple unrelated concerns: separate into logical commits133- Examples of good separation:134 - Documentation changes vs code changes135 - Feature additions vs bug fixes136 - Refactoring vs new functionality137 - Config changes vs source changes1381395. **Create commits**140141- Write clear, concise commit messages142- Use conventional commit style when appropriate (feat:, fix:, docs:, refactor:, etc.)143- Add `Co-Authored-By: Claude <noreply@anthropic.com>` if Claude contributed significantly144- Use HEREDOC format for commit messages:145146 ```bash147 git commit -m "$(cat <<'EOF'148 Commit message here149150 Co-Authored-By: Claude <noreply@anthropic.com>151 EOF152 )"153 ```1541556. **Push (only if `push` argument is present)**156157- Check commits ahead: `git rev-list --count @{upstream}..HEAD 2>/dev/null || echo "unknown"`158- If 0 commits ahead: nothing to push, skip to Verify159- If `"unknown"` (no upstream / new branch): run `git push -u origin $(git branch --show-current)`, skip to Verify160- **Check whether the branch is actually behind before pulling anything:**161162 ```bash163 git fetch164 git rev-list --count HEAD..@{upstream} # 0 = not behind165 ```166167- **If 0 (not behind): run `git push` directly. Do NOT pull.** A pull here is a no-op at best; at worst `--rebase` replays every local commit onto the remote tip, which **flattens any merge commit on the branch and re-applies its contents as fresh work** — that manufactures conflicts (typically in lockfiles) on a branch that had nothing to reconcile.168- If non-zero (genuinely behind): run `git pull --no-rebase` (regular merge — the default strategy per the user's global CLAUDE.md; `--rebase` would rewrite local history) then `git push`169- If the pull fails with a conflict, follow the Conflict Handling section above1701717. **Verify**172173- Run `git status` after committing to confirm working tree is clean174- Run `git log --oneline -n <number of commits>` to show what was committed175176## Important Notes177178- Never use `git add .` blindly - be selective about what to stage179- If unsure whether a file should be committed, ask the user180- Prefer smaller, focused commits over large monolithic commits181- Always verify the commit was successful before moving on182- **Never use `git commit --amend` without explicit user permission** - Always create new commits by default. Amending commits can be confusing and may cause issues if the original commit was already shared. If you need to fix a previous commit, ask the user first.183- **Proactively update .gitignore** - If you see files that should be ignored (node_modules, .env, logs, etc.), update .gitignore before committing. This prevents accidental commits of sensitive or unnecessary files in the future. Exception: never add image glob patterns (*.png, etc.) — delete or move those instead (see above).