Sub topic 5
Installing git filter-repo and starting from a fresh clone
This chapter covers installing git filter-repo and preparing the working copy it will rewrite. The setup takes only a few commands, but it decides whether the later rewrite is safe and repeatable.
This chapter assumes you can clone, commit, branch, and push with Git, and that commits are identified by hashes. It also assumes you are comfortable in a terminal and have permission to force-push to the affected repository. You will not rewrite anything yet. The next chapters do that.
What you are installing
git filter-repo is a history-rewriting tool. It is not part of core Git, and the Git project documents it as the replacement for git filter-branch. It is a single Python script named git-filter-repo. Git treats any executable named git-<name> on your PATH as a subcommand, so once the script is installed, git filter-repo works.
It has two requirements:
- Python 3. The script uses only the standard library, so there are no extra Python packages to install.
- A recent Git. The minimum version rises over time. Check the project README for the current number, because an older Git can fail with confusing errors.
Check both before you start:
git --version
python3 --versionInstalling it
Pick one method. They all produce the same command.
| Method | Command | Notes |
|---|---|---|
| pip | `pip install git-filter-repo` | Works on Linux, macOS, and Windows. This is the method the tutorial uses. |
| Homebrew | `brew install git-filter-repo` | macOS and Linux. |
| Debian or Ubuntu | `sudo apt install git-filter-repo` | The packaged version may lag the latest release. |
| Fedora | `sudo dnf install git-filter-repo` | Same caveat about version lag. |
| Manual | Copy `git-filter-repo` into `$(git --exec-path)` | Useful on locked-down machines with no package manager. |
The manual method is a single file copy. Download the script from the project repository, make it executable, and put it where Git looks for subcommands:
chmod +x git-filter-repo
sudo cp git-filter-repo "$(git --exec-path)"If you install with pip as a normal user, the script lands in a user-level scripts directory such as ~/.local/bin. That directory must be on your PATH. Confirm the install worked:
git filter-repo --version
git filter-repo --help | head -20If Git answers git: 'filter-repo' is not a git command, the script is not on your PATH. Run pip show -f git-filter-repo to see where pip put it, then add that directory to PATH.
On Windows, the script's first line asks for python3. If that name does not resolve on your machine, git filter-repo can fail even though the file exists. Installing with pip generates a launcher that avoids this problem, which is one reason pip is the safest choice there.
Why a fresh clone
git filter-repo rewrites history in place and discards the old objects. A mistake on your only copy of a repository could lose work that exists nowhere else. The tool therefore refuses to run unless the repository looks like a fresh clone. Several things make a clone "fresh":
- The reflog has at most one entry per ref. Real day-to-day work leaves many.
- There are no stashes.
- There are no uncommitted or untracked changes.
- The objects are packed, as they are right after a clone.
If any of these checks fail, the tool aborts with a message that says the repository does not look like a fresh clone. It names --force as the override. Do not use --force as a shortcut. The checks are the reason you can trust that every commit you are about to destroy also exists on the remote. A fresh clone proves that, and a repository with local-only commits or stashes does not.
A fresh clone also keeps the blast radius small. Your everyday working copy, with its branches, stashes, and half-finished work, stays untouched. If the rewrite goes wrong, you delete the clone and try again.
Making the clone
Clone into a new directory with a name that tells you its purpose. Use the same URL you normally use, so the clone has the same access:
pip install git-filter-repo
git clone [email protected]:org/repo.git repo-clean
cd repo-cleanKeeping the working directory name separate from the original (repo-clean, not repo) prevents you from running a destructive command in the wrong place. Check the directory name before every command that rewrites history.
What a normal clone does and does not contain
A plain git clone downloads the following:
- Every branch on the remote, as remote-tracking refs such as
refs/remotes/origin/main. Git creates a local branch only for the default branch. - Every tag.
- All commits reachable from those refs.
It does not download refs that the hosting platform keeps outside branches and tags. The most common are pull request refs, for example refs/pull/* on GitHub. Those refs can still hold commits that contain your secret, and the next chapters on hosting residue cover them. Do not assume that a clean rewrite of your clone cleans everything the host stores.
When it runs in a fresh clone, git filter-repo converts the remote-tracking branches it rewrites into local branches. This makes the later push simple, because every branch is a local ref.
Do you need a mirror clone?
git clone --mirror copies all refs, including ones outside refs/heads and refs/tags. For this tutorial's workflow, a plain clone is the right choice. Hosting platforms generally reject pushes to their internal refs, so a mirror push often fails with errors about hidden or protected refs. A plain clone gives you the branches and tags you can actually push.
Check the clone before you rewrite
Spend a minute looking at what you have. These commands change nothing:
git status
git branch -a
git tag | wc -l
git count-objects -vH
git log --all --oneline | wc -lWrite down the number of commits from the last command. After the rewrite you will compare it, because a rewrite that removes a file or replaces text normally keeps the commit count the same. A tool option can drop commits that become empty, so a lower number needs an explanation.
Also look at the size reported by count-objects. In the analysis chapter you will use it to decide which files are worth cleaning.
Keep a backup
The remote is your backup until you force-push, but a local copy is cheap insurance and does not depend on network access. Before running any rewrite, copy the clone:
cd ..
cp -R repo-clean repo-clean-backup
cd repo-cleanKeep the backup outside any directory that you will later push, and do not delete it until you have verified the rewritten history and pushed it. The backup contains the secret, so treat it with the same care as the original repository. Delete it when the cleanup is finished.
What the tool does to the clone
Two behaviors surprise people the first time:
- It removes the
originremote. This is deliberate. It stops you from pushing the rewritten history to the original location by accident. You re-add the remote later, as a conscious step. - It expires the reflog and runs garbage collection. After a run, the old commits are not reachable through the reflog in this clone. You cannot undo the rewrite with
git reflog. Your backup and the remote are the ways to recover.
Neither of these is a problem as long as you know to expect them. The force-push chapter shows the remote step.
Common errors
git: 'filter-repo' is not a git command. The script is not onPATH, or it is not executable. Fix thePATHor runchmod +xon the script.- Python errors on the first line, such as
python3: not found. Python 3 is missing or has a different name on your system. Install Python 3, or reinstall with pip. Refusing to destructively overwrite repo history since this does not look like a fresh clone. You are in a working copy that has history of its own. Make a fresh clone instead of adding--force.- The command ran, but the push fails with
No configured push destination. The tool removedorigin. This is expected, and you add the remote back yourself.
Exercise
A teammate says: "My repository already has a stash and some local branches. I'll just run git filter-repo --force in it so I don't have to clone again." Give two reasons this is a bad idea, and say what to do instead.
Show answer
First, the freshness checks exist so that you only destroy commits that also exist on the remote. A repository with a stash or local-only branches may hold work that exists nowhere else, and the rewrite and garbage collection will discard it for good, with no reflog to recover from. Second, the working copy is your only unmodified record of the original history. If the rewrite removes the wrong thing, you have no easy way to retry. Instead, push or save any local-only work, make a new clone in a separate directory such as repo-clean, optionally copy it as a backup, and run the tool there.
Exercise
After running git filter-repo in a fresh clone, git push origin main fails because there is no origin. Why did this happen, and is it a bug?
Show answer
It is intended behavior. git filter-repo removes the origin remote after it rewrites history, so that you cannot push the rewritten history to the original repository by accident. You add the remote back yourself with git remote add origin followed by the URL, then force-push as a deliberate step.