AgentSkillsCN

Git Master Skill

Git大师技能

SKILL.md

Git Master Skill

Advanced git operations and workflow management.

Trigger

  • Complex git operations
  • Branch management
  • Merge conflict resolution
  • Git history manipulation

Instructions

When this skill is invoked, you handle advanced git workflows.

Safety First

NEVER run without explicit user confirmation:

  • git push --force
  • git reset --hard
  • git rebase on shared branches
  • Any history-rewriting operation

ALWAYS:

  • Check current branch before operations
  • Verify remote status
  • Create backup branches for risky operations

Common Operations

Clean Commit History

bash
# Interactive rebase for cleaning commits
git rebase -i HEAD~n

# Squash commits
git reset --soft HEAD~n
git commit -m "Combined commit message"

Branch Management

bash
# Create and switch to new branch
git checkout -b feature/name

# Update branch with latest main
git fetch origin
git rebase origin/main

# Clean up merged branches
git branch --merged | grep -v "main\|master" | xargs git branch -d

Conflict Resolution

  1. Identify conflicts: git status
  2. Open conflicted files
  3. Choose correct resolution
  4. Stage resolved files: git add <file>
  5. Continue: git rebase --continue or git merge --continue

Undo Operations

bash
# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1

# Undo a specific commit (creates new commit)
git revert <commit-hash>

# Recover deleted branch
git reflog
git checkout -b recovered-branch <commit-hash>

Commit Message Guidelines

code
<type>(<scope>): <subject>

<body>

<footer>

Types:

  • feat: New feature
  • fix: Bug fix
  • refactor: Code restructuring
  • docs: Documentation
  • test: Tests
  • chore: Maintenance

Example:

code
feat(auth): add JWT token refresh

Implement automatic token refresh when access token
expires. Refresh happens 5 minutes before expiration.

Closes #123

PR Workflow

bash
# Update feature branch
git fetch origin
git rebase origin/main

# Push (with lease for safety)
git push --force-with-lease

# Create PR
gh pr create --title "feat: description" --body "..."

Stash Operations

bash
# Stash with message
git stash push -m "WIP: feature description"

# List stashes
git stash list

# Apply specific stash
git stash apply stash@{n}

# Pop and delete
git stash pop

Bisect for Bug Finding

bash
# Start bisect
git bisect start

# Mark current as bad
git bisect bad

# Mark known good commit
git bisect good <commit>

# Test each commit, mark good/bad
git bisect good  # or git bisect bad

# End bisect
git bisect reset

Worktree for Parallel Work

bash
# Create worktree for different branch
git worktree add ../project-hotfix hotfix-branch

# List worktrees
git worktree list

# Remove worktree
git worktree remove ../project-hotfix

Recovery Patterns

Lost Commits

bash
# Find in reflog
git reflog

# Cherry-pick or checkout
git cherry-pick <commit>

Corrupted Repository

bash
# Verify integrity
git fsck --full

# Recover from remote
git fetch origin
git reset --hard origin/main

Best Practices

  1. Commit Often: Small, focused commits
  2. Write Good Messages: Future you will thank you
  3. Branch Per Feature: Keep main clean
  4. Rebase Before PR: Clean history
  5. Never Force Push Main: Protect shared branches
  6. Use Tags for Releases: git tag -a v1.0.0 -m "Release 1.0.0"

Dangerous Operations Checklist

Before running destructive commands:

  • On correct branch?
  • Have backup/can recover?
  • Others affected?
  • User explicitly confirmed?