Rewriting History: Authors, Dates, Secrets and Big Files With filter-repo
Rewrite a whole repository's history safely: rotating leaked secrets first, git filter-repo for removing files, replacing text, fixing authors and extracting folders, rebase --exec for recent commits, the fast-export pipeline underneath, and cleaning up servers and clones.
kvi5hnuCode Sprout team member8 min readOn this page
Most history editing is local and small: amending the last commit, or tidying a branch with interactive rebase (articles 6 and 15). Sometimes, though, a whole repository's history needs changing: a password was committed two years ago, a 500 MB video is in every clone, everyone's commits carry an old company email, or one folder should become its own project. These rewrites touch every commit after the first one affected, give them all new IDs, and need coordination with everyone who has a clone. This article covers how to do them correctly, and what to clean up afterwards.
Before you rewrite
- A leaked secret must be revoked first. Once a password, token or key has been pushed, assume it's been copied: bots scan public repositories within minutes, and every existing clone, fork and cache still has it. Rotate the credential immediately. Rewriting history comes second, to stop the old value spreading further.
- Rewriting is disruptive. Every collaborator must re-clone or carefully reset, and open pull requests and branches based on the old history become hard to merge. For an author's name or email, a
.mailmapfile corrects how history is displayed without rewriting anything (article 32). - Make a backup:
git clone --mirror <url> backup.git, or a bundle (article 46).
git filter-repo
git filter-repo is the recommended tool for whole-repository rewrites. It's a separate Python script, installed with pip install git-filter-repo, brew install git-filter-repo or your distribution's package, and Git's own documentation recommends it over the old built-in git filter-branch, which is slow and full of pitfalls.
filter-repo works on a fresh clone, and refuses to run in a repository that doesn't look like one, so you can't damage your only copy by accident. It also removes the origin remote afterwards, so that you can't push by reflex.
git clone https://github.com/acme/trail-guide.git cleanup
cd cleanup
Removing a file or folder from all history
git filter-repo --path config.env --invert-paths # remove one file everywhere
git filter-repo --path secrets/ --path '*.pem' --invert-paths # several paths at once
git filter-repo --path-glob '*.mp4' --invert-paths # by pattern
--path selects paths, and --invert-paths means "keep everything except these". Commits that become empty are removed.
Removing big files
git filter-repo --analyze # reports in .git/filter-repo/analysis/
git filter-repo --strip-blobs-bigger-than 10M # remove every file version over 10 MB
--analyze lists sizes by path, extension and folder, including deleted files, which shows what's worth removing. To keep large files but move them to LFS, use git lfs migrate import instead (article 43).
Replacing text, such as a leaked password
Create a file of replacements, one per line:
sk_live_4f9a2c
password123==>REMOVED
regex:api_key=\w+==>api_key=REMOVED
Then run git filter-repo --replace-text replacements.txt. A line without ==> is replaced with ***REMOVED***. The files stay, but the secret is gone from every version of them. --replace-message does the same for commit messages.
Fixing authors and emails
Sam Lee <sam@example.com> <sam@old-company.example>
Ana Diaz <ana@example.com> Ana <ana@laptop.local>
git filter-repo --mailmap my-mailmap rewrites names and emails using the .mailmap format: the correct identity first, then the one to replace. --use-mailmap uses the repository's own .mailmap file.
Extracting or moving folders
git filter-repo --subdirectory-filter libs/maps # keep only libs/maps, and make it the root
git filter-repo --to-subdirectory-filter maps # move everything into maps/, before merging into another repo
git filter-repo --path-rename old-name/:new-name/
--subdirectory-filter turns one folder into a repository of its own, with its history, like git subtree split (article 42). --to-subdirectory-filter is the reverse: preparing a project to be merged into a monorepo without its files colliding.
What happens underneath
filter-repo is built on two core Git commands: git fast-export, which writes a repository's entire history as a text stream, and git fast-import, which builds commits from such a stream. The same pipeline works with a simple sed, which shows exactly what a rewrite does:
git init -q trail-guide && cd trail-guide
echo "Trail app" > app.txt; git add .; git commit -q -m "Start app"
echo "MAP_API_KEY=sk_live_4f9a2c" > config.env; git add .; git commit -q -m "Add config"
echo "Better maps" >> app.txt; git commit -q -am "Improve app"
git rm -q config.env; git commit -q -m "Remove config"
echo "=== original history; the key is still in it"
git log --oneline
echo "lines with the key in history: $(git log -p | grep -c sk_live)"
cd ..; git init -q cleaned; cd trail-guide
git fast-export --all | sed 's/sk_live_4f9a2c/***REMOVED***/' | git -C ../cleaned fast-import --quiet
cd ../cleaned; git reset -q --hard main
echo "=== rewritten history"
git log --oneline
echo "lines with the key in history: $(git log -p | grep -c sk_live)"
git show HEAD~2:config.env
=== original history; the key is still in it
d9a1bbb Remove config
6771f63 Improve app
3b51c94 Add config
7c1620d Start app
lines with the key in history: 2
=== rewritten history
32b0aab Remove config
af0fa8d Improve app
4b611a4 Add config
7c1620d Start app
lines with the key in history: 0
MAP_API_KEY=***REMOVED***
- Commits from the first changed one onwards get new IDs, because their content changed and each ID covers the parent's ID too (article 48). "Start app", before the key appeared, keeps its ID.
- The old repository is untouched: rewriting creates new commits, and the old ones stay wherever they already exist. That's why every clone of the old history must be dealt with.
- A plain
sedlike this is fragile, because it could change binary data or the stream's own commands. filter-repo parses the stream properly and handles tags, merges, empty commits and references for you.
Fixing the last few commits instead
For unpushed commits, or a short branch, interactive rebase is simpler than filter-repo. To fix the author of every commit on a branch, for example after committing with the wrong email:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git config user.email "sam@laptop.local" # wrong email for two commits
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Ridge loop" > ridge.txt; git add .; git commit -q -m "Add ridge page"
git log --format='%h %ae %s'
git config user.email "sam@example.com" # fix the setting, then the commits
git rebase -q HEAD~2 --exec 'git commit -q --amend --no-edit --reset-author'
echo "=== after the rebase"
git log --format='%h %ae %s'
538e45a sam@laptop.local Add ridge page
92fdb1b sam@laptop.local Add lake page
7fb62a0 sam@example.com Start project
=== after the rebase
cb59481 sam@example.com Add ridge page
1501041 sam@example.com Add lake page
7fb62a0 sam@example.com Start project
--exec runs a command after each commit is replayed, and git commit --amend --reset-author sets the author to your current identity and the author date to now. Use --root instead of HEAD~2 to include the first commit. To change only dates, git rebase --committer-date-is-author-date or --reset-author-date adjust them during the replay. For a single commit in the middle of a branch, the experimental git history commands do it without a todo list (article 15).
After a rewrite: servers, clones and caches
-
Push the rewritten history, replacing everything on the server:
bashgit remote add origin https://github.com/acme/trail-guide.git git push --force --mirror origin # or: git push --force --all && git push --force --tagsBranch protection must allow force pushes for this, at least temporarily.
-
Everyone re-clones. Old clones still contain the removed data, and pushing or merging from them brings it back. A fresh clone is the reliable fix. For someone with unpushed work, rebase it onto the new history with
git rebase --onto. -
The hosting service may still have it. Pull request references (such as GitHub's
refs/pull/*) can't be force pushed, cached views of old commits can stay reachable by ID, and forks keep their own copies. For leaked sensitive data, contact the host's support to remove cached data, and follow its guide. -
Clean up local copies that you keep, so the old objects are really gone:
bashgit reflog expire --expire=now --all git gc --prune=nowfilter-repo does this for you in the repository it rewrote.
Other tools
- BFG Repo-Cleaner is a Java tool for the two most common jobs, deleting files and replacing text. It's fast and simple, but less flexible than filter-repo.
git filter-branchis still included in Git, but prints a warning recommending filter-repo, and is much slower and easy to get wrong.
What's current
- git filter-repo is the tool recommended by Git's documentation for history rewrites, and
filter-branchis discouraged. - Hosting services offer secret scanning and push protection that block pushes containing known credential formats, which prevents many leaks before they need rewriting.
- Recent Git releases improved how
fast-exportandfast-importhandle signed commits: since a rewritten commit can't keep a valid signature,fast-importcan drop invalid signatures (--signed-commits=strip-if-invalid) or re-sign commits.
Common questions
I pushed a password and deleted it in the next commit. Is that enough?
No. The password is still in the earlier commit, and anyone can see it with git log -p. Revoke it first, then decide whether to rewrite history, which is worthwhile mainly to stop it spreading further.
filter-repo says "Refusing to destructively overwrite repo history since this does not look like a fresh clone". Why?
It protects you from rewriting a repository that might have unpushed work. Make a fresh clone and run it there, adding --no-local when cloning from a local path, so that objects are copied rather than hard-linked. --force overrides the check, if you're sure.
After the rewrite, a colleague pushed and the big file came back. What happened? Their clone still had the old history, and pushing or merging from it re-added the old commits. Rewrite again, and make sure everyone re-clones before pushing.
Do I need to rewrite history to fix an author's name in git log?
No. A .mailmap file maps old names and emails to the correct ones in log, shortlog and blame, without changing any commit (article 32).
Summary
- Rewriting history gives every affected commit a new ID and forces every clone to be replaced. Revoke leaked secrets first, and back up.
git filter-reporuns on a fresh clone:--path ... --invert-pathsremoves files,--strip-blobs-bigger-thanremoves big ones,--replace-textremoves secrets,--mailmapfixes identities, and--subdirectory-filterextracts a folder.- It's built on
git fast-export | git fast-import. - For recent commits,
git rebase --exec 'git commit --amend --no-edit --reset-author'fixes authors. - Afterwards: force push everything, have everyone re-clone, ask the host to purge caches for sensitive data, and expire reflogs and garbage-collect local copies.
Practice questions
A database password was committed in settings.ini eight months ago and pushed. List the steps, in order.
Answer
- Change the password in the database, and anywhere else it's used, immediately.
- Make a fresh clone with
git clone <url> cleanup. - Remove it with
git filter-repo --replace-text replacements.txt, listing the password, or remove the file withgit filter-repo --path settings.ini --invert-paths. - Add the remote and force push all branches and tags.
- Ask everyone to re-clone, and contact the hosting service to purge cached data.
- Add
settings.inito.gitignore, and use environment variables or a secrets manager from now on.
Why do all commits after the changed one get new IDs, even those that didn't touch the file?
Answer
A commit's ID is a hash of its content, which includes its parent's ID. Once one commit changes, its ID changes, so its child's recorded parent changes, so the child's ID changes, and so on to every descendant.
You committed three unpushed commits with your personal email instead of your work email. What's the quickest fix?
Answer
Set the correct email with git config user.email you@work.example, then run git rebase HEAD~3 --exec 'git commit --amend --no-edit --reset-author'. Since the commits aren't pushed, no one else is affected and there's no need for filter-repo.
Review quiz
Test yourself on Part 10 with the Git Part 10 review quiz.
Previous: Bundles: Moving Commits Without a Network Connection

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.