Claude
Skills
Sign in
Back

git-advanced

Included with Lifetime
$97 forever

Advanced git workflows including rebase, worktrees, bisect, hooks, and monorepo patterns

devtoolsgitversion-controlrebaseworktreesbisecthooksmonorepo

What this skill does


# Git Advanced Skill

Master advanced git workflows for efficient version control. This skill covers interactive rebase, worktrees, bisect, rerere, reflog, hooks, and patterns for monorepos and submodules.

## When to Use This Skill

### USE when:
- Managing complex branch strategies
- Working on multiple features simultaneously
- Hunting down bugs with bisect
- Maintaining clean commit history
- Setting up team workflows with hooks
- Managing multi-repo dependencies
- Recovering from git mistakes

### DON'T USE when:
- Simple linear development (basic git suffices)
- Solo projects with simple history
- When team isn't familiar with advanced git
- Time-critical fixes (use simple commits)

## Prerequisites

### Installation

**Git Configuration:**
```bash
# Verify git version (2.23+ recommended)
git --version

# Global configuration
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Recommended settings
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global push.autoSetupRemote true
git config --global rerere.enabled true
git config --global core.autocrlf input
git config --global core.editor "vim"

# Better diff
git config --global diff.algorithm histogram
git config --global merge.conflictStyle diff3
```

**GitHub CLI (optional):**
```bash
# macOS
brew install gh

# Linux
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install gh

# Authenticate
gh auth login
```

**Pre-commit (for hooks):**
```bash
# Install pre-commit
pip install pre-commit

# Or with homebrew
brew install pre-commit
```

## Core Capabilities

### 1. Interactive Rebase

**Basic Interactive Rebase:**
```bash
# Rebase last 5 commits
git rebase -i HEAD~5

# Rebase onto main
git rebase -i main

# Rebase from specific commit
git rebase -i abc123^
```

**Interactive Rebase Commands:**
```
pick   abc123 First commit     # Use commit as-is
reword def456 Second commit    # Edit commit message
edit   ghi789 Third commit     # Stop to amend
squash jkl012 Fourth commit    # Combine with previous
fixup  mno345 Fifth commit     # Combine, discard message
drop   pqr678 Sixth commit     # Remove commit
```

**Common Rebase Workflows:**
```bash
# Squash all commits into one
git rebase -i main
# Change all but first 'pick' to 'squash'

# Reorder commits
git rebase -i HEAD~3
# Rearrange the pick lines

# Split a commit
git rebase -i HEAD~3
# Change 'pick' to 'edit' on target commit
git reset HEAD^
git add -p  # Add pieces
git commit -m "First part"
git add .
git commit -m "Second part"
git rebase --continue

# Edit commit message
git rebase -i HEAD~3
# Change 'pick' to 'reword'
```

**Autosquash Pattern:**
```bash
# Create fixup commit
git commit --fixup=abc123

# Create squash commit
git commit --squash=abc123

# Apply autosquash
git rebase -i --autosquash main

# Enable autosquash by default
git config --global rebase.autosquash true
```

### 2. Git Worktrees

**Basic Worktree Usage:**
```bash
# List worktrees
git worktree list

# Add worktree for existing branch
git worktree add ../feature-branch feature/new-feature

# Add worktree with new branch
git worktree add -b hotfix/urgent ../hotfix-urgent main

# Remove worktree
git worktree remove ../feature-branch

# Prune stale worktrees
git worktree prune
```

**Worktree Workflow:**
```bash
# Project structure
workspace/
├── main/           # Main development
├── feature-auth/   # Feature branch
├── hotfix-critical/# Hotfix branch
└── experiment/     # Experimental work

# Setup
cd main
git worktree add ../feature-auth feature/authentication
git worktree add ../hotfix-critical -b hotfix/critical-bug
git worktree add ../experiment -b experiment/new-approach

# Work on feature
cd ../feature-auth
# make changes...
git commit -am "Add authentication"

# Quick switch to fix bug
cd ../hotfix-critical
# fix bug...
git commit -am "Fix critical bug"
git push

# Back to feature
cd ../feature-auth
```

**Worktree Helper Script:**
```bash
#!/bin/bash
# scripts/worktree.sh
# ABOUTME: Git worktree management helper
# ABOUTME: Simplifies creating and managing worktrees

set -e

WORKTREE_BASE="${WORKTREE_BASE:-$(dirname $(git rev-parse --git-dir))}"

case "$1" in
    add)
        BRANCH="$2"
        DIR="${3:-$WORKTREE_BASE/../$(echo $BRANCH | tr '/' '-')}"
        if git show-ref --verify --quiet "refs/heads/$BRANCH"; then
            git worktree add "$DIR" "$BRANCH"
        else
            git worktree add -b "$BRANCH" "$DIR"
        fi
        echo "Created worktree at: $DIR"
        ;;
    remove)
        git worktree remove "$2"
        ;;
    list)
        git worktree list
        ;;
    *)
        echo "Usage: $0 {add|remove|list} [branch] [directory]"
        exit 1
        ;;
esac
```

### 3. Git Bisect

**Basic Bisect:**
```bash
# Start bisect
git bisect start

# Mark current version as bad
git bisect bad

# Mark known good commit
git bisect good v1.0.0

# Git checks out a commit - test it
# If bad:
git bisect bad
# If good:
git bisect good

# Continue until found
# Git will show: "abc123 is the first bad commit"

# End bisect
git bisect reset
```

**Automated Bisect:**
```bash
# Create test script
cat > test-bug.sh << 'EOF'
#!/bin/bash
# Return 0 if good, non-zero if bad
npm test -- --grep "specific test"
EOF
chmod +x test-bug.sh

# Run automated bisect
git bisect start
git bisect bad HEAD
git bisect good v1.0.0
git bisect run ./test-bug.sh

# Git will find the bad commit automatically
git bisect reset
```

**Bisect with Skip:**
```bash
# If commit can't be tested (won't build)
git bisect skip

# Skip range of commits
git bisect skip abc123..def456
```

**Bisect Log and Replay:**
```bash
# Save bisect session
git bisect log > bisect.log

# Replay session
git bisect replay bisect.log
```

### 4. Git Rerere (Reuse Recorded Resolution)

**Enable Rerere:**
```bash
# Enable globally
git config --global rerere.enabled true

# Check status
git config --get rerere.enabled
```

**Using Rerere:**
```bash
# When you resolve a conflict, git records the resolution
git merge feature-branch
# Resolve conflicts...
git add .
git commit

# Next time same conflict occurs, git auto-applies resolution
git merge another-branch
# "Resolved 'file.txt' using previous resolution."

# If auto-resolution is wrong, forget it
git rerere forget path/to/file
```

**Rerere Management:**
```bash
# View recorded resolutions
ls .git/rr-cache/

# Clean old resolutions
git rerere gc

# Show diff of recorded resolution
git rerere diff
```

### 5. Git Reflog

**Basic Reflog:**
```bash
# Show reflog
git reflog

# Show reflog with dates
git reflog --date=relative

# Show reflog for specific ref
git reflog show feature-branch

# Output
# abc123 HEAD@{0}: commit: Latest commit
# def456 HEAD@{1}: checkout: moving from main to feature
# ghi789 HEAD@{2}: commit: Previous commit
```

**Recovery with Reflog:**
```bash
# Recover deleted branch
git reflog
# Find last commit of deleted branch: abc123
git checkout -b recovered-branch abc123

# Undo hard reset
git reflog
# Find state before reset: HEAD@{2}
git reset --hard HEAD@{2}

# Recover lost stash
git fsck --unreachable | grep commit
git show <commit-hash>
git stash apply <commit-hash>

# Recover from bad rebase
git reflog
# Find pre-rebase state: HEAD@{5}
git reset --hard HEAD@{5}
```

**Reflog Expiration:**
```bash
# Check expiration settings
git config --get gc.reflogexpire  # Default: 90 days
git config --get gc.reflogexpireunreachable  # Default: 30 days

# Extend reflog retention
git config --global gc.reflogexpire 180.days
```

### 6. Git Hooks

**Available Hooks:**
```
client-side:
  pre-commit      # Before commit message 

Related in devtools