$npx -y skills add softspark/ai-toolkit --skill git-masteryAdvanced Git: rebase, bisect, reflog, cherry-pick, worktrees, LFS. Triggers: rebase, bisect, cherry-pick, reflog, force push, merge conflict, worktree.
| 1 | # Git Mastery Skill |
| 2 | |
| 3 | ## 🛡️ Safety First Protocol |
| 4 | - **Never** force push to `main` / `master` / `develop`. |
| 5 | - **Always** use `--force-with-lease` instead of `--force`. |
| 6 | - **Always** create a backup branch before complex operations: |
| 7 | `git branch backup/feature-xyz-pre-rebase` |
| 8 | |
| 9 | ## Advanced Workflows |
| 10 | |
| 11 | ### 1. Automated Bug Hunting (Git Bisect) |
| 12 | Find the specific commit that introduced a bug. |
| 13 | ```bash |
| 14 | # Start |
| 15 | git bisect start |
| 16 | git bisect bad # Current version is broken |
| 17 | git bisect good <commit-sha> # Version that worked |
| 18 | |
| 19 | # Automate with test script |
| 20 | git bisect run pytest tests/test_failing_feature.py |
| 21 | ``` |
| 22 | |
| 23 | ### 2. Interactive Interactive Rebase |
| 24 | Clean up commit history before merge. |
| 25 | ```bash |
| 26 | git rebase -i HEAD~n |
| 27 | ``` |
| 28 | - **squash**: Combine commits. |
| 29 | - **reword**: Fix messages. |
| 30 | - **dropped**: Remove junk commits. |
| 31 | |
| 32 | ### 3. Log Analysis |
| 33 | Visualize branch topology. |
| 34 | ```bash |
| 35 | git log --graph --oneline --decorate --all |
| 36 | ``` |
| 37 | |
| 38 | ### 4. Recovery (Reflog) |
| 39 | Recover "lost" commits after a bad reset/rebase. |
| 40 | ```bash |
| 41 | git reflog |
| 42 | git reset --hard HEAD@{n} |
| 43 | ``` |
| 44 | |
| 45 | ### 5. Cherry Picking |
| 46 | Pick specific commits from other branches. |
| 47 | ```bash |
| 48 | git cherry-pick <commit-sha> |
| 49 | # If valid conflict |
| 50 | git add . |
| 51 | git cherry-pick --continue |
| 52 | ``` |
| 53 | |
| 54 | ## Commit Message Standard (Conventional Commits) |
| 55 | - `feat:` New feature |
| 56 | - `fix:` Bug fix |
| 57 | - `docs:` Documentation only |
| 58 | - `style:` Formatting (white-space, etc) |
| 59 | - `refactor:` Code change that neither fixes a bug nor adds a feature |
| 60 | - `perf:` Performance improvement |
| 61 | - `test:` Adding missing tests |
| 62 | - `chore:` Build process/auxiliary tools |
| 63 | |
| 64 | ## Common Rationalizations |
| 65 | |
| 66 | | Excuse | Why It's Wrong | |
| 67 | |--------|----------------| |
| 68 | | "I'll clean up commits later" | Later means never — write clean commits as you go | |
| 69 | | "Force push is fine on my branch" | Others may have fetched your branch — use --force-with-lease | |
| 70 | | "One big commit is simpler" | Big commits are impossible to review, bisect, or revert — keep them atomic | |
| 71 | | "Merge conflicts mean someone else's problem" | Conflicts mean you diverged too long — rebase frequently to stay aligned | |
| 72 | | "Commit messages don't matter" | Messages are documentation — future you needs to understand why, not just what | |
| 73 | |
| 74 | ## Rules |
| 75 | |
| 76 | - **MUST** use `--force-with-lease` instead of `--force` for any force operation on a shared branch |
| 77 | - **MUST** create a backup branch before interactive rebase, reset --hard, or filter-repo: `git branch backup/<name>-pre-rebase` |
| 78 | - **NEVER** force-push to `main`, `master`, or `develop` — these are shared trunk branches by convention |
| 79 | - **NEVER** rewrite history that has already been pushed AND consumed by others — you will orphan their clones |
| 80 | - **CRITICAL**: the reflog is a 90-day safety net (`gc.reflogExpire`) — tag anything you want to keep longer. Do not rely on reflog for multi-month recovery. |
| 81 | - **MANDATORY**: commit messages follow Conventional Commits (`feat:`, `fix:`, `docs:`, `refactor:`, `test:`, `chore:`) — drift defeats automated changelog and release tooling |
| 82 | |
| 83 | ## Gotchas |
| 84 | |
| 85 | - `git bisect` relies on each commit being testable. Flaky tests corrupt the bisection silently — a flaky "bad" mark sends `bisect` down the wrong half. Run the test 3× at the boundary commits if flakiness is known. |
| 86 | - `git cherry-pick <sha>` of a merge commit fails without `--mainline 1` (or 2). The error is cryptic ("commit is a merge but no -m option was given"); the fix is simple but non-obvious. |
| 87 | - `git reflog` entries expire by default in 90 days (`gc.reflogExpire`) and unreferenced commits get garbage-collected after 30 days (`gc.reflogExpireUnreachable`). Long-term recovery of "lost" commits from reflog is not guaranteed. |
| 88 | - `git rebase -i --root` is supposed to include the very first commit, but on **shallow clones** (`--depth N`) the "root" is the shallow boundary, not the actual initial commit. Run `git fetch --unshallow` before rebasing --root. |
| 89 | - `git filter-branch` is **deprecated** and slow (shell-based, re-forks per commit). Use `git filter-repo` for history rewriting — it is a separate tool (`pip install git-filter-repo`) but 100× faster and endorsed by the Git maintainers. |
| 90 | - `git pull --rebase` on a branch with unpushed merge commits rewrites those merges into linear history, silently losing the merge metadata. If a merge was intentional (e.g., to preserve feature-branch context), use `git pull --no-rebase` or set `pull.ff=only` globally. |
| 91 | |
| 92 | ## When NOT to Load |
| 93 | |
| 94 | - For simple commits on a ready branch — use `/commit` |
| 95 | - For opening a PR after commits are clean — use `/pr` |
| 96 | - For a specific failed operation needing root-cause analysis — use `/debug` on the git output |
| 97 | - For CI-specific git behavior (shallow clones, LFS on runners) — use `/ci-cd-patterns` |
| 98 | - For the in-repo `.git/hooks/*` content — this skill is user-facing Git; ho |