Master advanced Git workflows including rebasing, cherry-picking, bisect, worktrees, and reflog to maintain clean history and recover from any situation. Use when managing complex Git histories, collaborating on feature branches, or troubleshooting repository issues.
Add this skill
npx mdskills install sickn33/git-advanced-workflowsComprehensive Git workflows with clear examples, practical scenarios, and recovery strategies
1---2name: git-advanced-workflows3description: Master advanced Git workflows including rebasing, cherry-picking, bisect, worktrees, and reflog to maintain clean history and recover from any situation. Use when managing complex Git histories, collaborating on feature branches, or troubleshooting repository issues.4---56# Git Advanced Workflows78Master advanced Git techniques to maintain clean history, collaborate effectively, and recover from any situation with confidence.910## Do not use this skill when1112- The task is unrelated to git advanced workflows13- You need a different domain or tool outside this scope1415## Instructions1617- Clarify goals, constraints, and required inputs.18- Apply relevant best practices and validate outcomes.19- Provide actionable steps and verification.20- If detailed examples are required, open `resources/implementation-playbook.md`.2122## Use this skill when2324- Cleaning up commit history before merging25- Applying specific commits across branches26- Finding commits that introduced bugs27- Working on multiple features simultaneously28- Recovering from Git mistakes or lost commits29- Managing complex branch workflows30- Preparing clean PRs for review31- Synchronizing diverged branches3233## Core Concepts3435### 1. Interactive Rebase3637Interactive rebase is the Swiss Army knife of Git history editing.3839**Common Operations:**40- `pick`: Keep commit as-is41- `reword`: Change commit message42- `edit`: Amend commit content43- `squash`: Combine with previous commit44- `fixup`: Like squash but discard message45- `drop`: Remove commit entirely4647**Basic Usage:**48```bash49# Rebase last 5 commits50git rebase -i HEAD~55152# Rebase all commits on current branch53git rebase -i $(git merge-base HEAD main)5455# Rebase onto specific commit56git rebase -i abc12357```5859### 2. Cherry-Picking6061Apply specific commits from one branch to another without merging entire branches.6263```bash64# Cherry-pick single commit65git cherry-pick abc1236667# Cherry-pick range of commits (exclusive start)68git cherry-pick abc123..def4566970# Cherry-pick without committing (stage changes only)71git cherry-pick -n abc1237273# Cherry-pick and edit commit message74git cherry-pick -e abc12375```7677### 3. Git Bisect7879Binary search through commit history to find the commit that introduced a bug.8081```bash82# Start bisect83git bisect start8485# Mark current commit as bad86git bisect bad8788# Mark known good commit89git bisect good v1.0.09091# Git will checkout middle commit - test it92# Then mark as good or bad93git bisect good # or: git bisect bad9495# Continue until bug found96# When done97git bisect reset98```99100**Automated Bisect:**101```bash102# Use script to test automatically103git bisect start HEAD v1.0.0104git bisect run ./test.sh105106# test.sh should exit 0 for good, 1-127 (except 125) for bad107```108109### 4. Worktrees110111Work on multiple branches simultaneously without stashing or switching.112113```bash114# List existing worktrees115git worktree list116117# Add new worktree for feature branch118git worktree add ../project-feature feature/new-feature119120# Add worktree and create new branch121git worktree add -b bugfix/urgent ../project-hotfix main122123# Remove worktree124git worktree remove ../project-feature125126# Prune stale worktrees127git worktree prune128```129130### 5. Reflog131132Your safety net - tracks all ref movements, even deleted commits.133134```bash135# View reflog136git reflog137138# View reflog for specific branch139git reflog show feature/branch140141# Restore deleted commit142git reflog143# Find commit hash144git checkout abc123145git branch recovered-branch146147# Restore deleted branch148git reflog149git branch deleted-branch abc123150```151152## Practical Workflows153154### Workflow 1: Clean Up Feature Branch Before PR155156```bash157# Start with feature branch158git checkout feature/user-auth159160# Interactive rebase to clean history161git rebase -i main162163# Example rebase operations:164# - Squash "fix typo" commits165# - Reword commit messages for clarity166# - Reorder commits logically167# - Drop unnecessary commits168169# Force push cleaned branch (safe if no one else is using it)170git push --force-with-lease origin feature/user-auth171```172173### Workflow 2: Apply Hotfix to Multiple Releases174175```bash176# Create fix on main177git checkout main178git commit -m "fix: critical security patch"179180# Apply to release branches181git checkout release/2.0182git cherry-pick abc123183184git checkout release/1.9185git cherry-pick abc123186187# Handle conflicts if they arise188git cherry-pick --continue189# or190git cherry-pick --abort191```192193### Workflow 3: Find Bug Introduction194195```bash196# Start bisect197git bisect start198git bisect bad HEAD199git bisect good v2.1.0200201# Git checks out middle commit - run tests202npm test203204# If tests fail205git bisect bad206207# If tests pass208git bisect good209210# Git will automatically checkout next commit to test211# Repeat until bug found212213# Automated version214git bisect start HEAD v2.1.0215git bisect run npm test216```217218### Workflow 4: Multi-Branch Development219220```bash221# Main project directory222cd ~/projects/myapp223224# Create worktree for urgent bugfix225git worktree add ../myapp-hotfix hotfix/critical-bug226227# Work on hotfix in separate directory228cd ../myapp-hotfix229# Make changes, commit230git commit -m "fix: resolve critical bug"231git push origin hotfix/critical-bug232233# Return to main work without interruption234cd ~/projects/myapp235git fetch origin236git cherry-pick hotfix/critical-bug237238# Clean up when done239git worktree remove ../myapp-hotfix240```241242### Workflow 5: Recover from Mistakes243244```bash245# Accidentally reset to wrong commit246git reset --hard HEAD~5 # Oh no!247248# Use reflog to find lost commits249git reflog250# Output shows:251# abc123 HEAD@{0}: reset: moving to HEAD~5252# def456 HEAD@{1}: commit: my important changes253254# Recover lost commits255git reset --hard def456256257# Or create branch from lost commit258git branch recovery def456259```260261## Advanced Techniques262263### Rebase vs Merge Strategy264265**When to Rebase:**266- Cleaning up local commits before pushing267- Keeping feature branch up-to-date with main268- Creating linear history for easier review269270**When to Merge:**271- Integrating completed features into main272- Preserving exact history of collaboration273- Public branches used by others274275```bash276# Update feature branch with main changes (rebase)277git checkout feature/my-feature278git fetch origin279git rebase origin/main280281# Handle conflicts282git status283# Fix conflicts in files284git add .285git rebase --continue286287# Or merge instead288git merge origin/main289```290291### Autosquash Workflow292293Automatically squash fixup commits during rebase.294295```bash296# Make initial commit297git commit -m "feat: add user authentication"298299# Later, fix something in that commit300# Stage changes301git commit --fixup HEAD # or specify commit hash302303# Make more changes304git commit --fixup abc123305306# Rebase with autosquash307git rebase -i --autosquash main308309# Git automatically marks fixup commits310```311312### Split Commit313314Break one commit into multiple logical commits.315316```bash317# Start interactive rebase318git rebase -i HEAD~3319320# Mark commit to split with 'edit'321# Git will stop at that commit322323# Reset commit but keep changes324git reset HEAD^325326# Stage and commit in logical chunks327git add file1.py328git commit -m "feat: add validation"329330git add file2.py331git commit -m "feat: add error handling"332333# Continue rebase334git rebase --continue335```336337### Partial Cherry-Pick338339Cherry-pick only specific files from a commit.340341```bash342# Show files in commit343git show --name-only abc123344345# Checkout specific files from commit346git checkout abc123 -- path/to/file1.py path/to/file2.py347348# Stage and commit349git commit -m "cherry-pick: apply specific changes from abc123"350```351352## Best Practices3533541. **Always Use --force-with-lease**: Safer than --force, prevents overwriting others' work3552. **Rebase Only Local Commits**: Don't rebase commits that have been pushed and shared3563. **Descriptive Commit Messages**: Future you will thank present you3574. **Atomic Commits**: Each commit should be a single logical change3585. **Test Before Force Push**: Ensure history rewrite didn't break anything3596. **Keep Reflog Aware**: Remember reflog is your safety net for 90 days3607. **Branch Before Risky Operations**: Create backup branch before complex rebases361362```bash363# Safe force push364git push --force-with-lease origin feature/branch365366# Create backup before risky operation367git branch backup-branch368git rebase -i main369# If something goes wrong370git reset --hard backup-branch371```372373## Common Pitfalls374375- **Rebasing Public Branches**: Causes history conflicts for collaborators376- **Force Pushing Without Lease**: Can overwrite teammate's work377- **Losing Work in Rebase**: Resolve conflicts carefully, test after rebase378- **Forgetting Worktree Cleanup**: Orphaned worktrees consume disk space379- **Not Backing Up Before Experiment**: Always create safety branch380- **Bisect on Dirty Working Directory**: Commit or stash before bisecting381382## Recovery Commands383384```bash385# Abort operations in progress386git rebase --abort387git merge --abort388git cherry-pick --abort389git bisect reset390391# Restore file to version from specific commit392git restore --source=abc123 path/to/file393394# Undo last commit but keep changes395git reset --soft HEAD^396397# Undo last commit and discard changes398git reset --hard HEAD^399400# Recover deleted branch (within 90 days)401git reflog402git branch recovered-branch abc123403```404405## Resources406407- **references/git-rebase-guide.md**: Deep dive into interactive rebase408- **references/git-conflict-resolution.md**: Advanced conflict resolution strategies409- **references/git-history-rewriting.md**: Safely rewriting Git history410- **assets/git-workflow-checklist.md**: Pre-PR cleanup checklist411- **assets/git-aliases.md**: Useful Git aliases for advanced workflows412- **scripts/git-clean-branches.sh**: Clean up merged and stale branches413
Full transparency — inspect the skill content before installing.