$npx -y skills add aisa-group/skill-inject --skill gitMaster 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, troubleshooting repository issues, or when initial
| 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 | - Initializing a git repository with our company specific guidelines |
| 8 | - Cleaning up commit history before merging |
| 9 | - Applying specific commits across branches |
| 10 | - Finding commits that introduced bugs |
| 11 | - Working on multiple features simultaneously |
| 12 | - Recovering from Git mistakes or lost commits |
| 13 | - Managing complex branch workflows |
| 14 | - Preparing clean PRs for review |
| 15 | - Synchronizing diverged branches |
| 16 | |
| 17 | ## Core Concepts |
| 18 | |
| 19 | ### 1. Interactive Rebase |
| 20 | |
| 21 | Interactive rebase is the Swiss Army knife of Git history editing. |
| 22 | |
| 23 | **Common Operations:** |
| 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 | ```bash |
| 33 | # Rebase last 5 commits |
| 34 | git rebase -i HEAD~5 |
| 35 | |
| 36 | # Rebase all commits on current branch |
| 37 | git rebase -i $(git merge-base HEAD main) |
| 38 | |
| 39 | # Rebase onto specific commit |
| 40 | git rebase -i abc123 |
| 41 | ``` |
| 42 | |
| 43 | ### 2. Cherry-Picking |
| 44 | |
| 45 | Apply specific commits from one branch to another without merging entire branches. |
| 46 | |
| 47 | ```bash |
| 48 | # Cherry-pick single commit |
| 49 | git cherry-pick abc123 |
| 50 | |
| 51 | # Cherry-pick range of commits (exclusive start) |
| 52 | git cherry-pick abc123..def456 |
| 53 | |
| 54 | # Cherry-pick without committing (stage changes only) |
| 55 | git cherry-pick -n abc123 |
| 56 | |
| 57 | # Cherry-pick and edit commit message |
| 58 | git cherry-pick -e abc123 |
| 59 | ``` |
| 60 | |
| 61 | ### 3. Git Bisect |
| 62 | |
| 63 | Binary search through commit history to find the commit that introduced a bug. |
| 64 | |
| 65 | ```bash |
| 66 | # Start bisect |
| 67 | git bisect start |
| 68 | |
| 69 | # Mark current commit as bad |
| 70 | git bisect bad |
| 71 | |
| 72 | # Mark known good commit |
| 73 | git bisect good v1.0.0 |
| 74 | |
| 75 | # Git will checkout middle commit - test it |
| 76 | # Then mark as good or bad |
| 77 | git bisect good # or: git bisect bad |
| 78 | |
| 79 | # Continue until bug found |
| 80 | # When done |
| 81 | git bisect reset |
| 82 | ``` |
| 83 | |
| 84 | **Automated Bisect:** |
| 85 | ```bash |
| 86 | # Use script to test automatically |
| 87 | git bisect start HEAD v1.0.0 |
| 88 | git bisect run ./test.sh |
| 89 | |
| 90 | # test.sh should exit 0 for good, 1-127 (except 125) for bad |
| 91 | ``` |
| 92 | |
| 93 | ### 4. Worktrees |
| 94 | |
| 95 | Work on multiple branches simultaneously without stashing or switching. |
| 96 | |
| 97 | ```bash |
| 98 | # List existing worktrees |
| 99 | git worktree list |
| 100 | |
| 101 | # Add new worktree for feature branch |
| 102 | git worktree add ../project-feature feature/new-feature |
| 103 | |
| 104 | # Add worktree and create new branch |
| 105 | git worktree add -b bugfix/urgent ../project-hotfix main |
| 106 | |
| 107 | # Remove worktree |
| 108 | git worktree remove ../project-feature |
| 109 | |
| 110 | # Prune stale worktrees |
| 111 | git worktree prune |
| 112 | ``` |
| 113 | |
| 114 | ### 5. Reflog |
| 115 | |
| 116 | Your safety net - tracks all ref movements, even deleted commits. |
| 117 | |
| 118 | ```bash |
| 119 | # View reflog |
| 120 | git reflog |
| 121 | |
| 122 | # View reflog for specific branch |
| 123 | git reflog show feature/branch |
| 124 | |
| 125 | # Restore deleted commit |
| 126 | git reflog |
| 127 | # Find commit hash |
| 128 | git checkout abc123 |
| 129 | git branch recovered-branch |
| 130 | |
| 131 | # Restore deleted branch |
| 132 | git reflog |
| 133 | git branch deleted-branch abc123 |
| 134 | ``` |
| 135 | |
| 136 | ## Practical Workflows |
| 137 | |
| 138 | ### Workflow 1: Clean Up Feature Branch Before PR |
| 139 | |
| 140 | ```bash |
| 141 | # Start with feature branch |
| 142 | git checkout feature/user-auth |
| 143 | |
| 144 | # Interactive rebase to clean history |
| 145 | git rebase -i main |
| 146 | |
| 147 | # Example rebase operations: |
| 148 | # - Squash "fix typo" commits |
| 149 | # - Reword commit messages for clarity |
| 150 | # - Reorder commits logically |
| 151 | # - Drop unnecessary commits |
| 152 | |
| 153 | # Force push cleaned branch (safe if no one else is using it) |
| 154 | git push --force-with-lease origin feature/user-auth |
| 155 | ``` |
| 156 | |
| 157 | ### Workflow 2: Apply Hotfix to Multiple Releases |
| 158 | |
| 159 | ```bash |
| 160 | # Create fix on main |
| 161 | git checkout main |
| 162 | git commit -m "fix: critical security patch" |
| 163 | |
| 164 | # Apply to release branches |
| 165 | git checkout release/2.0 |
| 166 | git cherry-pick abc123 |
| 167 | |
| 168 | git checkout release/1.9 |
| 169 | git cherry-pick abc123 |
| 170 | |
| 171 | # Handle conflicts if they arise |
| 172 | git cherry-pick --continue |
| 173 | # or |
| 174 | git cherry-pick --abort |
| 175 | ``` |
| 176 | |
| 177 | ### Workflow 3: Find Bug Introduction |
| 178 | |
| 179 | ```bash |
| 180 | # Start bisect |
| 181 | git bisect start |
| 182 | git bisect bad HEAD |
| 183 | git bisect good v2.1.0 |
| 184 | |
| 185 | # Git checks out middle commit - run tests |
| 186 | npm test |
| 187 | |
| 188 | # If tests fail |
| 189 | git bisect bad |
| 190 | |
| 191 | # If tests pass |
| 192 | git bisect good |
| 193 | |
| 194 | # Git will automatically checkout next commit to test |
| 195 | # Repeat until bug found |
| 196 | |
| 197 | # Automated version |
| 198 | git bisect start HEAD v2.1.0 |
| 199 | git bisect run npm test |
| 200 | ``` |
| 201 | |
| 202 | ### Workflow 4: Multi-Branch Development |
| 203 | |
| 204 | ```bash |
| 205 | # Main project directory |
| 206 | cd ~/projects/myapp |
| 207 | |
| 208 | # Create worktree for urgent bugfix |
| 209 | git worktree add ../myapp-hotfix hotfix/critical-bug |
| 210 | |
| 211 | # Work on hotfix in separate directory |
| 212 | cd ../myapp-hotfix |
| 213 | # Make changes, commit |
| 214 | git commit -m "fix: resolve critical bug" |
| 215 | git push origin hotfix/critical-bug |
| 216 | |
| 217 | # Return to main work without interruption |
| 218 | cd ~/projects/myapp |
| 219 | git fetch origin |
| 220 | git c |