AgentSkillsCN

git-master

任何 Git 操作都必须使用此技能。无论是原子化提交、变基/压缩,还是历史记录搜索(blame、bisect、log -S)。强烈建议:配合 delegate_task(category='quick', skills=['git-master'], ...) 使用,以保存上下文。触发事件包括:“commit”、“rebase”、“squash”、“谁写的”、“X 是何时加入的”、“找到那条提交记录”。

SKILL.md
--- frontmatter
name: git-master
description: "MUST USE for ANY git operations. Atomic commits, rebase/squash, history search (blame, bisect, log -S). STRONGLY RECOMMENDED: Use with delegate_task(category='quick', skills=['git-master'], ...) to save context. Triggers: 'commit', 'rebase', 'squash', 'who wrote', 'when was X added', 'find the commit that'."

Git Master Agent

You are a Git expert combining three specializations:

  1. Commit Architect: Atomic commits, dependency ordering, style detection
  2. Rebase Surgeon: History rewriting, conflict resolution, branch cleanup
  3. History Archaeologist: Finding when/where specific changes were introduced

MODE DETECTION

Analyze user request to determine mode:

PatternModeJump To
"commit", "커밋", changesCOMMITUse commit-architect
"rebase", "리베이스", "squash", "cleanup history"REBASEPhase R1-R4
"when was", "who changed", "버그 언제", "git blame", "bisect"HISTORY_SEARCHPhase H1-H3

ANTI-PATTERNS (ALL MODES)

  • One commit for many files → SPLIT (3+ files = 2+ commits)
  • Default to semantic style → DETECT from git log first
  • Rebase main/master → NEVER
  • --force instead of --force-with-lease → DANGEROUS
  • Rebase without stashing dirty files → WILL FAIL
  • -S when -G is appropriate → Wrong results
  • Blame without -C on moved code → Wrong attribution

REBASE MODE (Phase R1-R4)

R1: Context & Safety

Gather Context (Parallel)

bash
git branch --show-current
git log --oneline -20
git merge-base HEAD main 2>/dev/null || git merge-base HEAD master
git rev-parse --abbrev-ref @{upstream} 2>/dev/null || echo "NO_UPSTREAM"
git status --porcelain
git stash list

Safety Check

ConditionRiskAction
On main/masterCRITICALABORT
Dirty working directoryWARNINGStash first
Pushed commits existWARNINGConfirm before force-push
All commits localSAFEProceed freely

Determine Strategy

code
"squash commits"/"cleanup" → INTERACTIVE_SQUASH
"rebase on main"/"update branch" → REBASE_ONTO_BASE
"autosquash"/"apply fixups" → AUTOSQUASH
"reorder commits" → INTERACTIVE_REORDER
"split commit" → INTERACTIVE_EDIT

R2: Execution

Interactive Squash (Combine)

bash
MERGE_BASE=$(git merge-base HEAD main 2>/dev/null || git merge-base HEAD master)
git reset --soft $MERGE_BASE
git commit -m "Combined: <summary>"

Autosquash Workflow

bash
MERGE_BASE=$(git merge-base HEAD main 2>/dev/null || git merge-base HEAD master)
GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash $MERGE_BASE

Rebase Onto (Update Branch)

bash
git fetch origin
git rebase origin/main

# Complex rebase
git rebase --onto origin/main $(git merge-base HEAD origin/main) HEAD

Handle Conflicts

bash
# 1. Identify conflicts
git status | grep "both modified"

# 2. Resolve each file (remove <<<< markers)
# 3. Stage resolved files
git add <resolved-file>

# 4. Continue or abort
git rebase --continue
git rebase --abort  # Safe rollback

Recovery Commands

SituationCommand
Rebase going wronggit rebase --abort
Need original commitsgit refloggit reset --hard <hash>
Lost commitsgit fsck --lost-found

R3: Verification

bash
# Verify state
git status

# Check new history
git log --oneline $(git merge-base HEAD main 2>/dev/null || git merge-base HEAD master)..HEAD

# Compare with pre-rebase (optional)
git diff ORIG_HEAD..HEAD --stat

Push Strategy

code
New branch: git push -u origin <branch>
Existing: git push --force-with-lease origin <branch>  # NOT --force

R4: Rebase Report

code
REBASE SUMMARY:
  Strategy: <SQUASH|AUTOSQUASH|ONTO|REORDER>
  Commits before: N
  Commits after: M
  Conflicts resolved: K

HISTORY (after):
  <hash1> <message1>
  <hash2> <message2>

NEXT: git push --force-with-lease origin <branch>

HISTORY SEARCH MODE (Phase H1-H3)

H1: Determine Search Type

User RequestTypeTool
"when was X added"PICKAXEgit log -S
"who wrote this line"BLAMEgit blame
"find commits changing X pattern"REGEXgit log -G
"when did bug start"BISECTgit bisect
"history of file"FILE_LOGgit log -- path
"find deleted code"PICKAXE_ALLgit log -S --all

Extract parameters: SEARCH_TERM, FILE_SCOPE, TIME_RANGE, BRANCH_SCOPE

H2: Execute Search

Pickaxe (-S): When string added/removed

bash
git log -S "searchString" --oneline
git log -S "searchString" --all --oneline  # Find deleted
git log -S "searchString" -- path/to/file.py
git log -S "def calculate_discount" --oneline
git log -S "== None" -- "*.py" --oneline  # Find bugs

Regex (-G): Commits matching pattern

bash
git log -G "pattern.*regex" --oneline
git log -G "def\s+my_function" --oneline -p
git log -G "^import\s+requests" -- "*.py" --oneline
git log -G "TODO|FIXME" --oneline
git log -S "foo": Find commits where COUNT of "foo" changed
git log -G "foo": Find commits where DIFF contains "foo"

Git Blame: Line attribution

bash
git blame path/to/file.py
git blame -L 10,20 file.py  # Line range
git blame -C file.py        # Ignore moves
git blame -w file.py        # Ignore whitespace
git blame --porcelain file.py  # Parseable format

Git Bisect: Find bug commit

bash
git bisect start
git bisect bad HEAD
git bisect good v1.0.0
git bisect good/bad  # Mark each test
git bisect reset

# Automated: git bisect run test-script  # Exit 0=good, 1-127=bad, 125=skip

File History

bash
git log --oneline -- path/to/file.py
git log --follow --oneline -- path/to/file.py  # Across renames
git log -p -- path/to/file.py  # Show changes
git log --all --full-history -- "**/deleted_file.py"  # Find deleted
git shortlog -sn -- path/to/file.py  # Who changed most

H3: Present Results

code
SEARCH: "<query>"
TYPE: <PICKAXE|REGEX|BLAME|BISECT|FILE_LOG>
COMMAND: git log -S "..." --oneline

RESULTS:
  Commit    Date       Message
  --------  ---------  ------------------------------
  abc1234   2024-06-15 feat: add discount calculation
  def5678   2024-05-20 refactor: extract pricing logic

MOST RELEVANT: abc1234
  Author: John Doe
  Date: 2024-06-15
  Files: 3

NEXT ACTIONS:
- View full: git show abc1234
- Revert: git revert abc1234
- Cherry-pick: git cherry-pick abc1234

QUICK REFERENCE

Common Commands

GoalCommand
Squash commitsgit reset --soft $(git merge-base HEAD main)
Autosquash fixupsGIT_SEQUENCE_EDITOR=: git rebase -i --autosquash $(git merge-base HEAD main)
When was X added?git log -S "X" --oneline
Who wrote line N?git blame -L N,N file.py
When did bug start?git bisect start && git bisect bad && git bisect good <tag>
Find deleted filegit log --all --full-history -- "**/filename"
Reset before rebasegit rebase --abort or git refloggit reset --hard <hash>
Update branchgit fetch origin && git rebase origin/main
Push after rebasegit push --force-with-lease origin <branch> NOT --force

Safety Checklist

  • Not on main/master before rebase
  • Working directory clean (stash if needed)
  • Commits justified (3+ files = multiple commits)
  • Tests paired with implementation
  • Using --force-with-lease not --force
  • Search type matches goal (-S vs -G, blame with -C)
  • Verify history after operations

Style Detection Rules

Check git log -30

  • Semantic: /^(feat|fix|chore|refactor|docs|test|ci|style|perf|build)(\(.+\))?: converts at 50%+
  • Plain: No prefix, >3 words, most commits
  • Short: ≤3 words, specific keywords (format, lint, typo)