Git Mastery Skill
🛡️ Safety First Protocol
- Never force push to
main / master / develop.
- Always use
--force-with-lease instead of --force.
- Always create a backup branch before complex operations:
git branch backup/feature-xyz-pre-rebase
Advanced Workflows
1. Automated Bug Hunting (Git Bisect)
Find the specific commit that introduced a bug.
# Start
git bisect start
git bisect bad # Current version is broken
git bisect good <commit-sha> # Version that worked
# Automate with test script
git bisect run pytest tests/test_failing_feature.py
2. Interactive Interactive Rebase
Clean up commit history before merge.
git rebase -i HEAD~n
- squash: Combine commits.
- reword: Fix messages.
- dropped: Remove junk commits.
3. Log Analysis
Visualize branch topology.
git log --graph --oneline --decorate --all
4. Recovery (Reflog)
Recover "lost" commits after a bad reset/rebase.
git reflog
git reset --hard HEAD@{n}
5. Cherry Picking
Pick specific commits from other branches.
git cherry-pick <commit-sha>
# If valid conflict
git add .
git cherry-pick --continue
Commit Message Standard (Conventional Commits)
feat: New feature
fix: Bug fix
docs: Documentation only
style: Formatting (white-space, etc)
refactor: Code change that neither fixes a bug nor adds a feature
perf: Performance improvement
test: Adding missing tests
chore: Build process/auxiliary tools
Common Rationalizations
| Excuse |
Why It's Wrong |
| "I'll clean up commits later" |
Later means never — write clean commits as you go |
| "Force push is fine on my branch" |
Others may have fetched your branch — use --force-with-lease |
| "One big commit is simpler" |
Big commits are impossible to review, bisect, or revert — keep them atomic |
| "Merge conflicts mean someone else's problem" |
Conflicts mean you diverged too long — rebase frequently to stay aligned |
| "Commit messages don't matter" |
Messages are documentation — future you needs to understand why, not just what |
Rules
- MUST use
--force-with-lease instead of --force for any force operation on a shared branch
- MUST create a backup branch before interactive rebase, reset --hard, or filter-repo:
git branch backup/<name>-pre-rebase
- NEVER force-push to
main, master, or develop — these are shared trunk branches by convention
- NEVER rewrite history that has already been pushed AND consumed by others — you will orphan their clones
- 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.
- MANDATORY: commit messages follow Conventional Commits (
feat:, fix:, docs:, refactor:, test:, chore:) — drift defeats automated changelog and release tooling
Gotchas
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.
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.
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.
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.
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.
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.
When NOT to Load
- For simple commits on a ready branch — use
/commit
- For opening a PR after commits are clean — use
/pr
- For a specific failed operation needing root-cause analysis — use
/debug on the git output
- For CI-specific git behavior (shallow clones, LFS on runners) — use
/ci-cd-patterns
- For the in-repo
.git/hooks/* content — this skill is user-facing Git; hook mechanics live in /hook-creator and install_git_hooks.py
1---2name: git-mastery3description: "Advanced Git: rebase, bisect, reflog, cherry-pick, worktrees, LFS. Triggers: rebase, bisect, cherry-pick, reflog, force push, merge conflict, worktree."4effort: medium5user-invocable: false6allowed-tools: Read, Grep, Glob7---89# Git Mastery Skill1011## 🛡️ Safety First Protocol12- **Never** force push to `main` / `master` / `develop`.13- **Always** use `--force-with-lease` instead of `--force`.14- **Always** create a backup branch before complex operations:15 `git branch backup/feature-xyz-pre-rebase`1617## Advanced Workflows1819### 1. Automated Bug Hunting (Git Bisect)20Find the specific commit that introduced a bug.21```bash22# Start23git bisect start24git bisect bad # Current version is broken25git bisect good <commit-sha> # Version that worked2627# Automate with test script28git bisect run pytest tests/test_failing_feature.py29```3031### 2. Interactive Interactive Rebase32Clean up commit history before merge.33```bash34git rebase -i HEAD~n35```36- **squash**: Combine commits.37- **reword**: Fix messages.38- **dropped**: Remove junk commits.3940### 3. Log Analysis41Visualize branch topology.42```bash43git log --graph --oneline --decorate --all44```4546### 4. Recovery (Reflog)47Recover "lost" commits after a bad reset/rebase.48```bash49git reflog50git reset --hard HEAD@{n}51```5253### 5. Cherry Picking54Pick specific commits from other branches.55```bash56git cherry-pick <commit-sha>57# If valid conflict58git add .59git cherry-pick --continue60```6162## Commit Message Standard (Conventional Commits)63- `feat:` New feature64- `fix:` Bug fix65- `docs:` Documentation only66- `style:` Formatting (white-space, etc)67- `refactor:` Code change that neither fixes a bug nor adds a feature68- `perf:` Performance improvement69- `test:` Adding missing tests70- `chore:` Build process/auxiliary tools7172## Common Rationalizations7374| Excuse | Why It's Wrong |75|--------|----------------|76| "I'll clean up commits later" | Later means never — write clean commits as you go |77| "Force push is fine on my branch" | Others may have fetched your branch — use --force-with-lease |78| "One big commit is simpler" | Big commits are impossible to review, bisect, or revert — keep them atomic |79| "Merge conflicts mean someone else's problem" | Conflicts mean you diverged too long — rebase frequently to stay aligned |80| "Commit messages don't matter" | Messages are documentation — future you needs to understand why, not just what |8182## Rules8384- **MUST** use `--force-with-lease` instead of `--force` for any force operation on a shared branch85- **MUST** create a backup branch before interactive rebase, reset --hard, or filter-repo: `git branch backup/<name>-pre-rebase`86- **NEVER** force-push to `main`, `master`, or `develop` — these are shared trunk branches by convention87- **NEVER** rewrite history that has already been pushed AND consumed by others — you will orphan their clones88- **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.89- **MANDATORY**: commit messages follow Conventional Commits (`feat:`, `fix:`, `docs:`, `refactor:`, `test:`, `chore:`) — drift defeats automated changelog and release tooling9091## Gotchas9293- `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.94- `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.95- `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.96- `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.97- `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.98- `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.99100## When NOT to Load101102- For simple commits on a ready branch — use `/commit`103- For opening a PR after commits are clean — use `/pr`104- For a specific failed operation needing root-cause analysis — use `/debug` on the git output105- For CI-specific git behavior (shallow clones, LFS on runners) — use `/ci-cd-patterns`106- For the in-repo `.git/hooks/*` content — this skill is user-facing Git; hook mechanics live in `/hook-creator` and `install_git_hooks.py`