AgentSkillsCN

Git Helper

Git 助手

SKILL.md

Git Helper

Expert Git assistant for version control operations, troubleshooting, and best practices.

Core Capabilities

1. Branch Management

  • Create, switch, merge, and delete branches
  • Rebase vs merge decision guidance
  • Branch naming conventions
  • Feature branch workflows

2. Commit Operations

  • Interactive staging (git add -p)
  • Amending commits
  • Commit message best practices
  • Squashing commits
  • Cherry-picking specific commits

3. History Management

  • View and search commit history
  • Interactive rebase for cleaning history
  • Undoing commits (reset vs revert)
  • Recovering lost commits (reflog)

4. Conflict Resolution

  • Merge conflict resolution strategies
  • Understanding conflict markers
  • Using merge tools
  • Abort and retry strategies

5. Stash Operations

  • Stash changes temporarily
  • Apply, pop, and drop stashes
  • Stash with messages
  • Partial stashing

6. Remote Operations

  • Push, pull, fetch workflows
  • Tracking branches
  • Managing remotes
  • Force push safety

7. Advanced Operations

  • Bisect for bug hunting
  • Submodules management
  • Worktrees
  • Hooks setup

Common Workflows

Feature Branch Workflow

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

# Make changes and commit
git add .
git commit -m "feat: add new feature"

# Keep feature branch updated
git fetch origin
git rebase origin/main

# Push feature branch
git push -u origin feature/new-feature

Fixing Last Commit

bash
# Amend last commit message
git commit --amend -m "new message"

# Add forgotten files to last commit
git add forgotten-file.txt
git commit --amend --no-edit

Undoing Changes

bash
# Undo uncommitted changes to file
git checkout -- file.txt

# Unstage file
git reset HEAD file.txt

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

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

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

Conflict Resolution

bash
# When merge conflict occurs
git status  # See conflicted files

# Edit files to resolve conflicts, then:
git add resolved-file.txt
git commit

# Or abort merge
git merge --abort

Stash Workflow

bash
# Stash current changes
git stash save "WIP: feature X"

# List stashes
git stash list

# Apply most recent stash
git stash apply

# Apply and remove stash
git stash pop

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

# Drop stash
git stash drop stash@{0}

Interactive Rebase

bash
# Rebase last 3 commits
git rebase -i HEAD~3

# Commands:
# pick   = use commit
# reword = edit commit message
# edit   = stop to amend commit
# squash = combine with previous commit
# fixup  = like squash but discard message
# drop   = remove commit

Finding Lost Commits

bash
# View reflog
git reflog

# Recover lost commit
git checkout <commit-hash>
git cherry-pick <commit-hash>

Cleaning Repository

bash
# Show what would be deleted
git clean -n

# Remove untracked files
git clean -f

# Remove untracked files and directories
git clean -fd

# Remove ignored files too
git clean -fdx

Best Practices

Commit Messages

  • Use conventional commits: feat:, fix:, docs:, refactor:, etc.
  • First line: 50 chars max, imperative mood
  • Body: wrap at 72 chars, explain why
  • Reference issues: Fixes #123

Branch Naming

  • feature/description - New features
  • fix/description - Bug fixes
  • hotfix/description - Urgent production fixes
  • refactor/description - Code refactoring
  • docs/description - Documentation changes

Merge vs Rebase

  • Use merge for: public branches, feature PRs, preserving complete history
  • Use rebase for: personal branches, cleaning history, staying up to date with main

Safety Rules

  • Never rebase public/shared branches
  • Always pull before push
  • Use --force-with-lease instead of --force
  • Review changes before force push
  • Keep commits atomic and focused

Troubleshooting

"Detached HEAD" State

bash
# Create branch from current state
git checkout -b new-branch-name

# Or return to previous branch
git checkout main

Pushed Wrong Commit

bash
# Revert publicly (safe)
git revert <bad-commit-hash>
git push

# Force push (only if no one pulled) - CAREFUL!
git reset --hard HEAD~1
git push --force-with-lease

Large File Committed

bash
# Remove from last commit
git rm --cached large-file.bin
git commit --amend --no-edit

# Remove from history (use BFG Repo-Cleaner or git-filter-repo)

Merge Conflicts Every Time

bash
# Use rerere (reuse recorded resolution)
git config rerere.enabled true

Git Aliases (Productivity Boosters)

bash
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual 'log --oneline --graph --decorate --all'
git config --global alias.amend 'commit --amend --no-edit'

Output Format

When helping with git operations:

  1. Understand the user's current git state
  2. Provide the appropriate git commands
  3. Explain what each command does
  4. Warn about destructive operations
  5. Suggest alternatives when applicable
  6. Show how to verify the result