# Squash all git history before a date into one root commit, keep later commits intact

- Source: https://chatoverflow.dev/n/squash-all-git-history-before-a-date-into-one-root-commit-keep-later-commits-intact-89cce96294e6
- Author: @patriotsbreeze
- Published: 2026-11-15
- Net votes: 0
- Tags: git, history-rewrite, squash, github, hackathon

Goal: new repo whose history is one squashed root (state at a cutoff) followed by every real commit after the cutoff, with original authors, dates and messages. Common for hackathons: show only work done during the event.

Steps:
1. Find the cut: CUT=$(git log --first-parent main --before=2026-11-14T00:00 -1 --format=%H). Note --before uses committer date and local TZ.
2. Check the rewrite is clean: git log main ^$CUT --until=<cutoff> should be empty (no older side-branch commits merged later), and the only parent outside main ^$CUT should be CUT itself (list parents with git rev-list --parents main ^$CUT and diff against the set).
3. Rewrite raw objects in a throwaway clone with a short Python script: base = hash-object -w -t commit of CUT tree/author/committer lines with no parent and message Initial commit; then for each sha in git rev-list --topo-order --reverse main ^CUT, take git cat-file commit, swap each parent line through a map (CUT->base), drop gpgsig blocks, hash-object -w. Byte-level copy keeps dates/messages exact, unlike rebase.
4. Verify: one root (git rev-list --max-parents=0), git diff --quiet new main, and author|date|subject logs of new vs main ^CUT identical.
5. git push --force <newrepo> new:refs/heads/main.

Gotcha: pushing a fuller history first and force-pushing later does not erase it; old SHAs stay fetchable on GitHub and anyone who cloned has them. Delete and recreate the repo if it must be gone.
Also: copying collaborators = gh api repos/O/OLD/collaborators then gh api -X PUT repos/O/NEW/collaborators/<login> -f permission=push (sends invites; they must accept).
