AgentSkillsCN

git-commit

在执行 Git 提交时,遵循正确的流程,包括身份验证、分支安全、原子提交以及风格分析。当您被要求提交、保存或持久化 Git 中的更改时,或在完成适合提交的任务后,又或是自主对工作进行检查点保存时,此技能将为您提供支持。此技能负责解答“如何提交”这一问题——涵盖工作流步骤、命令使用、分支管理等内容。

SKILL.md
--- frontmatter
name: git-commit
description: Execute Git commits with proper workflow including authentication, branch safety, atomic commits, and style analysis. Use when asked to commit, save, or persist changes to Git, after completing tasks where committing is logical, or when autonomously checkpointing work. This skill handles the HOW of committing (workflow steps, commands, branch management).

Git Commit Skill

Execute Git commits following a structured workflow that ensures authentication, branch safety, atomic commits, and project style consistency.

References

Workflow

Step 1: Verify GitHub CLI Authentication

Before any Git operation, verify authentication status:

bash
gh auth status

If authentication fails (exit code non-zero or error message contains "not logged in"):

  1. Inform the user: "GitHub CLI authentication is missing or expired."
  2. Provide the command to re-authenticate:
    bash
    gh auth login --web
    
  3. Wait for the user to complete authentication before proceeding.

Common authentication error patterns:

  • "You are not logged in"
  • "authentication required"
  • "token has expired"
  • "invalid token"
  • Exit code 1 with stderr output

Step 2: Identify Files to Commit

Atomic commits principle: Each commit should represent ONE logical change. Do not bundle unrelated changes into a single commit.

Analyze the current Git status to identify files to commit:

bash
# Show current status
git status --short

Not all changed files should be committed. Having a branch with multiple unrelated changes makes it hard to review, revert, or understand history. Try to determine what the user intends to commit based on context and recent changes. If unclear, ask the user for clarification on which files or changes to include in the commit.

bash
# Show detailed diff for unstaged changes
git diff

# Show detailed diff for staged changes
git diff --cached

Grouping strategy:

  1. Analyze all pending changes and identify logical groups
  2. Group by purpose: Files that serve the same change belong together
  3. Separate unrelated changes: Different features, fixes, or docs = separate commits
  4. Ask user if ambiguous: When grouping is unclear, ask for clarification

Examples of logical grouping:

ChangesCommits
New service + its unit tests1 commit: feat: add PaymentService
New feature + unrelated config change2 commits: feature first, then config
Bug fix in component + related test fix1 commit: fix: handle null in BillForm
Refactor + unrelated documentation update2 commits: refactor first, then docs
Multiple files for one feature (action, service, component)1 commit: all related files together

Staging rules:

User saysAction
"commit all changes"Group into logical atomic commits, create multiple if needed
"commit these files" + listgit add <file1> <file2>...
"commit staged files"Use already staged files
"commit <specific file>"git add <file>
AmbiguousAsk user to clarify which files and grouping

Step 3: Verify Branch Safety

CRITICAL: Never commit directly to protected branches.

Detect Protected Branch

bash
# Get the repository's default branch
gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name'

# Get current branch
git branch --show-current

Protected branches include:

  • The default branch (usually main or master)
  • develop or development
  • Any branch matching release/* or hotfix/* patterns

If on Protected Branch

  1. STOP - do not commit directly
  2. Inform the user: "You are on the protected branch <branch>. Creating a feature branch."
  3. Create an appropriately named feature branch based on analysis from Step 2:
bash
git checkout -b <type>/<short-description>

Branch Naming Convention

Format: <type>/<kebab-case-description>

TypeUse CaseExample
featNew featurefeat/bill-reminders
fixBug fixfix/null-amount-validation
refactorCode restructuringrefactor/extract-payment-service
choreMaintenance taskschore/update-dependencies
docsDocumentationdocs/api-reference
testTest additionstest/payment-service-coverage

Rules:

  • Use kebab-case (lowercase with hyphens)
  • Keep it short (2-4 words max)
  • Make it descriptive of the change intent
  • ALWAYS in English

Step 4: Analyze Project Writing Style

Analyze recent commits to match the project's vocabulary and phrasing:

bash
git log --format="%s" -30

You have to mimic the vocabulary, detail level, and phrasing style.

Writing style characteristics to identify:

CharacteristicWhat to Look For
VocabularyWhich verbs are commonly used? (add, implement, introduce, etc.)
Detail levelBrief ("fix bug") vs descriptive ("fix null pointer in auth flow")
Scope patternsCommon scopes: (deps), (api), (ui), or no scope
SpecificityGeneric vs domain-specific terminology

Important: The Conventional Commits format is fixed. Only adapt the vocabulary and phrasing style to match project conventions.

Identify:

  • Common verbs (add, implement, introduce, etc.)
  • Detail level (brief vs descriptive)
  • Scope usage patterns ((deps), (api), or none)
  • Domain-specific terminology

Mimic the identified style while following Conventional Commits format.

Step 5: Generate and Execute Commit

Stage files and create commit:

bash
# Stage specific files
git add <files>

# Create commit with message
git commit -m "<message>"

For multi-line messages:

bash
git commit -m "<subject>" -m "<body paragraph 1>" -m "<body paragraph 2>"

Follow the PR Description Structure from Commit Message Format.

Step 6: Confirm Success

Verify and report:

bash
# Show the created commit
git log --oneline -1

# Show commit details
git show --stat HEAD

Report to user:

  • Commit hash (short form)
  • Files changed count
  • Insertions/deletions summary

Error Handling

Git Errors

ErrorCauseResolution
"nothing to commit"No staged changesVerify files exist and have changes
"pathspec did not match"File path incorrectCheck file path spelling
"not a git repository"Not in repo directoryNavigate to correct directory

Authentication Errors

If any gh command fails with authentication error:

  1. Run gh auth status to diagnose
  2. If token expired: gh auth refresh
  3. If no token: Report the issue clearly to the user and offer to run gh auth login --web