git-history-surgery

Rewrites contaminated git history and re-anchors orphaned branches after force-push rewrites.

Updated Mar 22, 2026
One-click install
npx skills add https://github.com/diazMelgarejo/orama-system --skill git-history-surgery-diazmelgarejo
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: git-history-surgery
Source: https://github.com/diazMelgarejo/orama-system/tree/main/bin/orama-system/skills/git-history-surgery
Command: npx skills add https://github.com/diazMelgarejo/orama-system --skill git-history-surgery-diazmelgarejo

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Secrets, forbidden identities, or workstation paths sometimes land in committed git history, and history rewrites leave branches looking hundreds of commits behind or orphaned with no common ancestor. This Skill provides a single decision framework for safely scrubbing contaminated history, force-pushing with recorded leases, and recovering branches using byte-identical tree-twin verification instead of misleading ahead/behind counts. ## Core Features & Use Cases - Contaminated History Expunge: Step-by-step scrub sequence using filter-repo, reflog expiry, all-ref blob scanning with local-only pattern files, and clean replacement PRs when intermediate commits are poisoned. - Post-Rewrite Branch Recovery: Tree-twin classification (SAFE-BEHIND vs NEEDS-REANCHOR), cherry-reanchor automation, and patch-equivalence rebase for stacked PR families after sibling merges. - Safety Guardrails: Mandatory hooks-off surgery windows, pending-operation push guards, whole-file deletion preflight, and explicit --force-with-lease targets recorded before any force-push. - Use Case: After a secret leaks into 40 branches of commit history, disable hooks, run the expunge sequence, force-publish with recorded lease SHAs, run the all-ref blob scan to prove zero remaining hits, then re-anchor every stale branch and verify each PR with gh pr view before declaring completion. ## Quick Start Ask the agent to expunge a leaked secret from git history and recover the branches that now show hundreds of commits behind main.

Frequently Asked Questions about git-history-surgery

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I remove a secret from git history?

Use git filter-repo with a replace-text or message callback to scrub the secret from all commits, then expire reflogs, prune unreachable objects, and force-push with a recorded lease. Rotate the secret separately since rewriting history does not un-leak it.

Why do my branches show hundreds of commits behind after a history rewrite?

Rewritten ancestry changes every SHA, so merge-base and ahead/behind counts become meaningless. Classify branches with a tree-twin scan comparing tree hashes instead; branches with byte-identical trees to main are SAFE-BEHIND and need no re-anchor.

How do I force-push safely after rewriting git history?

Record the expected remote SHA first, then push with --force-with-lease=refs/heads/<branch>:<recorded-sha> so the push fails if the remote moved unexpectedly. Disable hooks during the surgery window with git -c core.hooksPath=/dev/null and restore them immediately after.

When should I not rewrite git history?

Avoid rewrites on shared long-lived branches with many collaborators, when rotation plus a forward fix is sufficient, or when you cannot coordinate a force-push window. A branch merely behind a normally advanced main needs an ordinary push, not surgery.

How do I verify a git history scrub actually removed the secret?

Run an all-ref blob scan over git rev-list --objects --all using a local-only pattern file that reports labels and counts, never literal values. Check HEAD, origin/main, and the PR-unique range separately; a clean working tree alone is not sufficient evidence.

Why did my push ship the wrong commit after resolving a merge?

A resolved but uncommitted --no-commit merge leaves MERGE_HEAD set locally, and git push ships the pre-merge commit without any error. Run the pending-operation guard checking MERGE_HEAD, CHERRY_PICK_HEAD, and REVERT_HEAD before every push.