Video in progress

Staff 🌿 Git

You have to rewrite the history of a repo used by the whole company (because of a secret or big files). How do you plan it?

I plan it like a deploy: an announcement in advance, a code freeze window, new history with git filter-repo, verification of the result, a force push, everyone cloning again, and blocking pushes from old clones. If it is a secret, rotate it first.

📖 Answer

Rewriting history changes every commit hash. That affects not only developers' local clones but everything that depends on the hashes: open PRs, commit links in Jira, deploy logs, versions based on git describe, submodule pointers, forks.

The plan I draw up:

  1. Is it needed? If it is a secret, rotation is the main protection and cleaning history is secondary. If it is size, a partial clone or starting from a new repo may be cheaper.
  2. A rehearsal. On a copy of the repo, run git filter-repo --path big-dumps/ --invert-paths (or --replace-text), then check the result: build, tests, whether the tags are in the right place.
  3. The announcement. Date, time, duration. People are asked to merge or close their open PRs beforehand.
  4. The freeze. Pushes are blocked temporarily.
  5. Execution. The new history is pushed (--force, all branches and tags), and the old→new hash mapping file is saved (filter-repo writes it to .git/filter-repo/commit-map).
  6. Protection. A pre-receive hook or scanner on the server so that objects with the old hashes do not come back. A single git push --all from an old clone undoes the whole job.
  7. Platform caches. On GitHub/GitLab, old commits may still be reachable through PRs, so clean them up with support.

In a very large organization, it is sometimes simpler to open a new repo and turn the old one into a read-only archive: fewer problems, and the history is in the archive if needed.