Sub topic 11
Re-adding the remote and force-pushing
This chapter covers the last step of the rewrite: publishing the cleaned history. It assumes you have a fresh clone, have run git filter-repo, and have verified that the secret no longer appears in any ref. It also assumes you have permission to force-push to the repository.
Why filter-repo removes the remote
After a rewrite, every affected commit has a new hash. If the tool left origin in place, a careless git push could mix old and new history on the host. To prevent that, git filter-repo deletes the origin remote when it finishes. This is a safety feature. Nothing can be pushed until you choose a destination on purpose.
Check the current state:
git remote -vEmpty output confirms the remote is gone.
Re-add the remote
Add the same URL you cloned from. Check it character by character. The next step overwrites whatever is at that address.
git remote add origin [email protected]:org/repo.git
git remote -vForce-push branches and tags
A normal push is rejected, because the new history is not a descendant of the old history. A force-push tells the host to move each ref to the new commit anyway.
git push --force --all origin
git push --force --tags origin--allpushes every local branch.--tagsis a separate command because--alldoes not include tags.- Both commands rewrite the refs on the host. Neither restores deleted content on the remote.
A safer variant is --force-with-lease. It refuses to push if the remote branch has moved since your last fetch, which protects commits a teammate pushed while you worked. A fresh clone has just fetched, so the lease is usually satisfied.
git push --force-with-lease --all originProtected branches
Hosts often block force-pushes to the default branch. If the push is rejected with a message about protected branches, you have two options.
- Ask an administrator to temporarily allow force-pushes for your account.
- Turn off the branch protection rule yourself, if you own it, push, and then turn it back on.
Restore the protection immediately after the push. Leaving it off invites accidents.
What the push does not do
The force-push updates the branches and tags you name. It does not touch everything else:
- Pull request refs such as
refs/pull/*can keep the old commits on some hosts. - Forks keep their own copy of the old history.
- Existing clones keep the old objects until their owners delete them.
The next chapters cover these leftovers. For now, remember that the push is necessary but does not complete the cleanup. This is also why the credential must already be rotated.
Flow of the whole step
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `fatal: 'origin' does not appear to be a git repository` | The remote was removed and not re-added. | Run `git remote add origin URL`. |
| `remote: error: GH006: Protected branch update failed` | Branch protection blocks force-pushes. | Allow force-pushes temporarily, then push again. |
| `stale info` from `--force-with-lease` | The remote changed after your clone. | Stop and find out who pushed. Re-clone and redo the rewrite if needed. |
| Old tags still show the secret | Tags were not pushed. | Run `git push --force --tags origin`. |
Exercise
After running git filter-repo, you run git push --force --all origin and the old tag v1.0 still points at a commit containing the secret on the host. What went wrong, and what do you run to fix it?
Show answer
--all pushes branches only, not tags. Run git push --force --tags origin to move the tags to the rewritten commits.
Exercise
Why does git filter-repo remove the origin remote, and why is that useful?
Show answer
Rewritten commits have new hashes. Removing origin stops an accidental plain push from mixing old and new history, so you must add the remote back deliberately before pushing.