$npx -y skills add wshobson/agents --skill git-advanced-workflowsMaster 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.
| 1 | # Git Advanced Workflows |
| 2 | |
| 3 | Master advanced Git techniques to maintain clean history, collaborate effectively, and recover from any situation with confidence. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Cleaning up commit history before merging |
| 8 | - Applying specific commits across branches |
| 9 | - Finding commits that introduced bugs |
| 10 | - Working on multiple features simultaneously |
| 11 | - Recovering from Git mistakes or lost commits |
| 12 | - Managing complex branch workflows |
| 13 | - Preparing clean PRs for review |
| 14 | - Synchronizing diverged branches |
| 15 | |
| 16 | ## Core Concepts |
| 17 | |
| 18 | ### 1. Interactive Rebase |
| 19 | |
| 20 | Interactive rebase is the Swiss Army knife of Git history editing. |
| 21 | |
| 22 | **Common Operations:** |
| 23 | |
| 24 | - `pick`: Keep commit as-is |
| 25 | - `reword`: Change commit message |
| 26 | - `edit`: Amend commit content |
| 27 | - `squash`: Combine with previous commit |
| 28 | - `fixup`: Like squash but discard message |
| 29 | - `drop`: Remove commit entirely |
| 30 | |
| 31 | **Basic Usage:** |
| 32 | |
| 33 | ```bash |
| 34 | # Rebase last 5 commits |
| 35 | git rebase -i HEAD~5 |
| 36 | |
| 37 | # Rebase all commits on current branch |
| 38 | git rebase -i $(git merge-base HEAD main) |
| 39 | |
| 40 | # Rebase onto specific commit |
| 41 | git rebase -i abc123 |
| 42 | ``` |
| 43 | |
| 44 | ### 2. Cherry-Picking |
| 45 | |
| 46 | Apply specific commits from one branch to another without merging entire branches. |
| 47 | |
| 48 | ```bash |
| 49 | # Cherry-pick single commit |
| 50 | git cherry-pick abc123 |
| 51 | |
| 52 | # Cherry-pick range of commits (exclusive start) |
| 53 | git cherry-pick abc123..def456 |
| 54 | |
| 55 | # Cherry-pick without committing (stage changes only) |
| 56 | git cherry-pick -n abc123 |
| 57 | |
| 58 | # Cherry-pick and edit commit message |
| 59 | git cherry-pick -e abc123 |
| 60 | ``` |
| 61 | |
| 62 | ### 3. Git Bisect |
| 63 | |
| 64 | Binary search through commit history to find the commit that introduced a bug. |
| 65 | |
| 66 | ```bash |
| 67 | # Start bisect |
| 68 | git bisect start |
| 69 | |
| 70 | # Mark current commit as bad |
| 71 | git bisect bad |
| 72 | |
| 73 | # Mark known good commit |
| 74 | git bisect good v1.0.0 |
| 75 | |
| 76 | # Git will checkout middle commit - test it |
| 77 | # Then mark as good or bad |
| 78 | git bisect good # or: git bisect bad |
| 79 | |
| 80 | # Continue until bug found |
| 81 | # When done |
| 82 | git bisect reset |
| 83 | ``` |
| 84 | |
| 85 | **Automated Bisect:** |
| 86 | |
| 87 | ```bash |
| 88 | # Use script to test automatically |
| 89 | git bisect start HEAD v1.0.0 |
| 90 | git bisect run ./test.sh |
| 91 | |
| 92 | # test.sh should exit 0 for good, 1-127 (except 125) for bad |
| 93 | ``` |
| 94 | |
| 95 | ### 4. Worktrees |
| 96 | |
| 97 | Work on multiple branches simultaneously without stashing or switching. |
| 98 | |
| 99 | ```bash |
| 100 | # List existing worktrees |
| 101 | git worktree list |
| 102 | |
| 103 | # Add new worktree for feature branch |
| 104 | git worktree add ../project-feature feature/new-feature |
| 105 | |
| 106 | # Add worktree and create new branch |
| 107 | git worktree add -b bugfix/urgent ../project-hotfix main |
| 108 | |
| 109 | # Remove worktree |
| 110 | git worktree remove ../project-feature |
| 111 | |
| 112 | # Prune stale worktrees |
| 113 | git worktree prune |
| 114 | ``` |
| 115 | |
| 116 | ### 5. Reflog |
| 117 | |
| 118 | Your safety net - tracks all ref movements, even deleted commits. |
| 119 | |
| 120 | ```bash |
| 121 | # View reflog |
| 122 | git reflog |
| 123 | |
| 124 | # View reflog for specific branch |
| 125 | git reflog show feature/branch |
| 126 | |
| 127 | # Restore deleted commit |
| 128 | git reflog |
| 129 | # Find commit hash |
| 130 | git checkout abc123 |
| 131 | git branch recovered-branch |
| 132 | |
| 133 | # Restore deleted branch |
| 134 | git reflog |
| 135 | git branch deleted-branch abc123 |
| 136 | ``` |
| 137 | |
| 138 | ## Detailed patterns and worked examples |
| 139 | |
| 140 | Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient. |
| 141 | |
| 142 | ## Best Practices |
| 143 | |
| 144 | 1. **Always Use --force-with-lease**: Safer than --force, prevents overwriting others' work |
| 145 | 2. **Rebase Only Local Commits**: Don't rebase commits that have been pushed and shared |
| 146 | 3. **Descriptive Commit Messages**: Future you will thank present you |
| 147 | 4. **Atomic Commits**: Each commit should be a single logical change |
| 148 | 5. **Test Before Force Push**: Ensure history rewrite didn't break anything |
| 149 | 6. **Keep Reflog Aware**: Remember reflog is your safety net for 90 days |
| 150 | 7. **Branch Before Risky Operations**: Create backup branch before complex rebases |
| 151 | |
| 152 | ```bash |
| 153 | # Safe force push |
| 154 | git push --force-with-lease origin feature/branch |
| 155 | |
| 156 | # Create backup before risky operation |
| 157 | git branch backup-branch |
| 158 | git rebase -i main |
| 159 | # If something goes wrong |
| 160 | git reset --hard backup-branch |
| 161 | ``` |
| 162 | |
| 163 | ## Common Pitfalls |
| 164 | |
| 165 | - **Rebasing Public Branches**: Causes history conflicts for collaborators |
| 166 | - **Force Pushing Without Lease**: Can overwrite teammate's work |
| 167 | - **Losing Work in Rebase**: Resolve conflicts carefully, test after rebase |
| 168 | - **Forgetting Worktree Cleanup**: Orphaned worktrees consume disk space |
| 169 | - **Not Backing Up Before Experiment**: Always create safety branch |
| 170 | - **Bisect on Dirty Working Directory**: Commit or stash before bisecting |
| 171 | |
| 172 | ## Recovery Commands |
| 173 | |
| 174 | ```bash |
| 175 | # Abort operations in progress |
| 176 | git rebase --abort |
| 177 | git merge --abort |
| 178 | git cherry-pick --abort |
| 179 | git bisect reset |
| 180 | |
| 181 | # Restore file to version from specific commit |
| 182 | git restore --source=abc123 path/to/file |
| 183 | |
| 184 | # Undo last commit but keep changes |
| 185 | git reset --soft HEAD^ |
| 186 | |
| 187 | # Undo last commit and discard changes |
| 188 | git reset --hard HEAD^ |
| 189 | |
| 190 | # Recover deleted branch (within 90 days) |
| 191 | git reflog |
| 192 | git |