Sub topic 11
Re-adding the remote and force-pushing
This chapter covers the last step of the rewrite: sending the cleaned history back to the hosting server. It assumes you have already rotated the leaked credential, run git filter-repo in a fresh clone, and verified that the secret no longer appears in any ref. It also assumes you have permission to force-push to the repository, and that you know what a remote and a branch are.
Why the push needs special handling
A rewrite changes the hash of every commit from the first modified commit onward. Every descendant commit changes too, because each hash includes its parent's hash. The cleaned branch therefore shares no commits with the version on the server after the first change. If your clone started from the same tip as the server, the old and new histories diverge at the earliest commit that touched the secret.
A normal git push refuses to do this. Git only accepts a push that fast-forwards, meaning the new tip must contain the old tip. A rewritten branch does not, so the server rejects it:
! [rejected] main -> main (non-fast-forward)A force-push tells the server to move the branch pointer to the new commit anyway. The old commits are not deleted at that moment. They become unreachable from the branch, which matters for the cleanup work later in this tutorial.
Why the remote is missing
After a run, git filter-repo deletes the origin remote from the repository's configuration. This is deliberate. A rewrite in a fresh clone is destructive to the shared history if pushed by accident, so the tool makes you opt in. It also converts the remote-tracking branches from the clone into local branches, so after the run each branch you cloned exists as a local branch.
Check the current state before pushing:
git remote -v
git branch --list
git tag --list | headThe first command prints nothing. The second lists the branches that were rewritten. Compare the branch and tag lists to what you expect on the server. A missing branch is cheaper to fix now than after the push.
Re-adding the remote
Add the remote back using the same URL you cloned from. Copy it from your original clone or from the hosting page, and check it character by character. Pushing a rewritten history to the wrong repository, such as a fork or a similarly named project, is hard to undo.
git remote add origin [email protected]:org/repo.git
git remote -vThe second command should print the URL twice, once for fetch and once for push.
Pushing branches and tags
Push branches and tags separately:
git push --force --all origin
git push --force --tags origin--allpushes every local branch underrefs/heads/. It does not push tags.--tagspushes everything underrefs/tags/. It does not push branches.--forceallows each ref to move to a commit that is not a descendant of the old one.
Tags need the second command because filter-repo rewrites annotated and lightweight tags to point at the new commits. An annotated tag is its own object that references a commit, so its hash changes as well. If you skip the tag push, the server keeps tags pointing at old commits that still contain the secret.
Safer variants
Force with a lease
Plain --force overwrites whatever is on the server. --force-with-lease overwrites it only if the server's branch still points where you expect. It protects you from erasing a commit someone pushed after you cloned.
Because the remote was removed and re-added, your clone has no remote-tracking information for it, so a bare --force-with-lease has nothing to compare against. Give the expected old hash explicitly:
git ls-remote origin refs/heads/main
git push --force-with-lease=main:OLD_SHA origin mainReplace OLD_SHA with the 40-character hash printed by ls-remote. If the server's main has moved since, the push fails and you know to re-run the rewrite from a newer clone. This costs one extra step per branch. Use it when the repository is active and you cannot guarantee a freeze.
Avoid `--mirror` on hosted platforms
git push --mirror pushes every ref and deletes remote refs that do not exist locally. On GitHub and similar hosts, the server keeps read-only refs such as refs/pull/*. A mirror push tries to update or delete them and is rejected for those refs, sometimes failing the whole push with deny updating a hidden ref. Prefer the two explicit commands above. Reserve --mirror for a self-hosted bare repository where you control every ref.
Branches that were not rewritten
If a branch was untouched by the rewrite, its hashes are unchanged and the push reports Everything up-to-date or skips it. That is expected. A branch is only rewritten if it contains a commit that matched your filter.
If you limited the rewrite with --refs, branches outside that scope keep the old history. Pushing them is harmless, but they may still contain the secret. Verify every branch on the server, not only the one you were working on.
Branch protection and push rules
Many hosts block force-pushes to the default branch. If you see messages like these, a protection rule is the cause:
remote: error: GH006: Protected branch update failed for refs/heads/main.
! [remote rejected] main -> main (protected branch hook declined)Work through the options in order:
- Ask an administrator to allow force-pushes for the branch temporarily.
- Turn off the "block force pushes" setting yourself, if you are an administrator.
- Push, then restore the setting immediately.
Do not leave the rule disabled. A server-side check such as a secret-scanning push rule can also reject the push if it still detects the secret in some commit. That is a useful failure: it means your verification missed something, such as a second copy in another branch or tag.
Other rejections you may meet:
| Message | Likely cause | Fix |
|---|---|---|
| `non-fast-forward` without `--force` | You forgot the flag | Add `--force` |
| `stale info` from `--force-with-lease` | The server branch moved | Re-clone, re-run the rewrite |
| `protected branch hook declined` | Branch protection | Temporarily allow force-push |
| `deny updating a hidden ref` | You used `--mirror` | Use `--all` and `--tags` |
| `Permission denied (publickey)` | Wrong key or user | Check `ssh -T` and the URL |
Checking the result on the server
A successful push message is not proof. Compare the server's refs to your local ones:
git ls-remote origin
git for-each-ref --format='%(objectname) %(refname)' refs/heads refs/tagsThe hashes for each branch and tag in the two outputs should match. ls-remote will also list server-only refs, such as refs/pull/*. Those are not under your control and are covered in the next chapter.
Then do the real test from the server's side. Clone to a new directory and search it, using the same commands you used for verification:
cd ..
git clone [email protected]:org/repo.git verify-clone
cd verify-clone
git log --all --oneline -S'AKIAEXAMPLEKEY12345'
git grep -I 'AKIAEXAMPLEKEY12345' $(git rev-list --all) || echo 'no matches'A clone fetches only what the server advertises, so this shows what a new contributor would see. Empty log output and no matches mean the visible refs are clean. They do not show what the server still stores behind unreferenced commits. That residue is why you rotated the credential first.
A short checklist
- Confirm the push freeze is in effect and collaborators know.
- Run
git remote -vand confirm it is empty after the rewrite. - Add
originwith the exact URL. - Push branches with
--force --all, or use--force-with-leaseper branch. - Push tags with
--force --tags. - Compare
git ls-remote originwith your local refs. - Clone fresh and repeat the secret search.
- Restore branch protection.
Exercise
After running git filter-repo in a fresh clone, you run git push --force --all origin and it succeeds. A teammate then runs git clone and finds that the tag v1.2 still points to a commit that contains the secret. What did you forget, and why was the tag affected?
Show answer
You forgot git push --force --tags origin. The --all option pushes only branches under refs/heads. Filter-repo rewrote the tag to point at a new, clean commit locally, but the server still had the old tag pointing at the old commit. Pushing the tags with --force moves the server's tag to the rewritten commit. A force is needed because the tag already exists with a different target.
Exercise
Why does a bare git push --force-with-lease give no useful protection right after you re-add origin, and what do you pass instead?
Show answer
The lease compares the server's branch to your remote-tracking branch. Filter-repo removed the remote, and the re-added remote has no tracking refs yet, so there is nothing to compare against. Pass the expected old hash explicitly, for example git push --force-with-lease=main:OLD_SHA origin main, where OLD_SHA comes from git ls-remote origin refs/heads/main.