Sub topic 5
Installing git filter-repo and starting from a fresh clone
This chapter assumes you know basic Git (clone, commit, branch, push), that commits are identified by hashes, and that you are comfortable on a command line. You also need permission to force-push to the affected repository, because the later chapters end with that step. By now you should have rotated the leaked credential. Rewriting history does not make a leaked secret safe, so never skip that step.
Why a fresh clone
git filter-repo rewrites every commit it touches. Each rewritten commit gets a new hash, and the old objects are discarded from the repository you run it in. If something goes wrong in your everyday working copy, you can lose unpushed branches, stashes, or local config.
The tool protects you from this. By default it refuses to run unless the repository looks like a fresh clone. It checks for signs such as a single packed set of objects, no stashes, and no extra local state. The --force flag overrides the check, but the check exists to protect you, so start clean instead.
A fresh clone gives you three things:
- Your original working copy stays untouched as a fallback.
- The clone contains only what the remote has, so nothing private or unpushed gets rewritten by accident.
- The result is easy to verify, because you can compare it against the original.
Install the tool
git filter-repo is a single Python script that Git picks up as a subcommand. It needs Git and Python 3 on your path. The simplest install is with pip:
pip install git-filter-repo
git clone [email protected]:org/repo.git repo-clean
cd repo-cleanCheck that Git can find it:
git filter-repo --versionIf the command is not found, make sure the directory where pip places scripts is on your PATH. Package managers such as Homebrew (brew install git-filter-repo) and most Linux distributions also ship it.
Clone for rewriting
The second and third commands above create the working directory. The name repo-clean is only a convention. It tells you this directory is for rewriting and not for daily work.
A normal git clone fetches all branches and tags, which is what you want here. Do not use --depth for a shallow clone, and do not use --single-branch. Either one would leave part of the history unrewritten, and the secret could remain in the parts you skipped.
git branch -a
git tag --listThese commands show the branches and tags that came with the clone. The branches listed under remotes/origin/ are copies of the remote's branches.
What the tool will do to this clone
Two behaviors matter before you run anything.
- It removes the
originremote. This is a safety measure that stops you from pushing rewritten history by accident. You will add the remote back deliberately in the force-push chapter. - It cleans up old objects. After the rewrite, the repository is repacked and the original commits are no longer reachable.
The diagram shows where this chapter fits in the workflow.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `git: 'filter-repo' is not a git command` | The script is not on `PATH` | Add the `pip` scripts directory to `PATH`, or reinstall |
| `Aborting: Refusing to destructively overwrite repo history` | The repository is not a fresh clone | Make a new clone instead of using `--force` |
| Old commits still contain the secret after a rewrite | The clone was shallow or single-branch | Re-clone with all branches and full history |
Exercise
Why does git filter-repo refuse to run in a repository you have been working in, and why should you not simply pass --force?
Show answer
The tool rewrites history and discards the old objects, so in a working copy with unpushed branches, stashes, or local state you could lose data permanently. The fresh-clone check is a safety guard. Making a new full clone keeps your original as a fallback and ensures only remote content is rewritten.