Sub topic 13
Telling Collaborators to Re-clone
This chapter assumes you have already rotated the leaked credential, rewritten history with git filter-repo, verified the result, and force-pushed it. It also assumes you know git fetch, git rebase, and git cherry-pick. The rewrite is not finished until everyone who cloned the repository has replaced their copy. This chapter covers how to tell them, what they should do, and how they keep their unpushed work.
Why old clones are a problem
A rewrite does not edit commits. It creates new commits with new hashes, and every commit from the first changed one onward gets a new identity. Your force-push moved the branches on the host to those new commits. Every existing clone still has the old commits, and the local main still points at them.
A collaborator who runs git pull then has two unrelated histories that contain the same changes. Git sees diverged branches and offers a merge. The merge succeeds, and the merge commit joins the old history to the new. The collaborator's next push is a plain fast-forward on top of the new remote tip, so the host accepts it without force. The old commits, including the one with the secret, are back on the remote.
The sequence below shows the order that avoids this.
The rotation you did first limits the damage here. The old clones hold a dead credential. The real risks are that a careless merge brings the old commits back, and that the repository ends up with duplicate history.
Before the rewrite: freeze and collect
Work done during the rewrite is the hardest to recover. Reduce it first.
- Pick a time window and announce it ahead of time.
- Ask everyone to push finished work and merge or close pull requests that are ready.
- Record the open pull requests and branches, so you can check afterward that none were lost.
- Check that your host's branch protection allows force-pushes for you. Many hosts block them on the default branch. Turn the protection back on after the push.
Work that is not ready can stay local. The patch steps below handle it.
The announcement
Send the message through a channel people read, such as a team chat channel or email. Post it again after the force-push, because the first message is easy to miss. Do not put the secret in the message. Say which kind of credential leaked, not its value.
Subject: Repository history rewritten. Re-clone required.
We removed a leaked credential from the history of org/repo.
The credential has already been revoked and replaced.
What happened: every commit from config/secrets.env's first
appearance onward now has a new hash. The new tip of main is
9f3c2ab (check with: git rev-parse --short origin/main).
What you must do:
1. Do NOT pull, merge, or push from your existing clone.
2. Save any unpushed commits as patches (steps in the wiki).
3. Clone the repository again into a new directory.
4. Re-apply your work, then delete the old clone.
Open pull requests: please rebase onto the new main
and push with --force-with-lease.Include the new tip hash so people can confirm they are looking at the rewritten history.
Re-clone, and keep the old copy until your work is safe
The simplest recovery is a fresh clone next to the old one. Do not delete the old directory yet. It may hold work that exists nowhere else.
cd ~/work
mv repo repo-old
git clone [email protected]:org/repo.git repoCheck the clone against the hash from the announcement.
cd repo
git rev-parse --short origin/mainCarrying over unpushed work
Look at three kinds of work in the old clone: committed but unpushed, uncommitted, and stashed.
For committed work, export patches from the old clone. The old origin/main still points at the old history, which gives you the right starting point.
cd ~/work/repo-old
git log --oneline origin/main..my-feature
git format-patch origin/main..my-feature -o ../patchesThen, in the new clone, create the branch from the new main and apply the patches.
cd ~/work/repo
git switch -c my-feature origin/main
git am --3way ../patches/*.patchThe --3way option helps when context lines changed because of a redaction. Search the patches for the secret before applying them. A patch that touched the leaked file may carry the old value.
grep -l 'AKIAEXAMPLEKEY12345' ../patches/*.patch || echo 'patches are clean'For uncommitted changes, save them as a diff or copy the changed files.
cd ~/work/repo-old
git diff > ../wip.diffFor stashes, convert each one to a patch with git stash show -p stash@{0} > ../stash0.diff, and apply it in the new clone with git apply.
A tempting shortcut is to add the old clone as a remote and cherry-pick from it. This works, but the fetch copies the old objects into the new clone's object store. Those objects are not pushed unless a ref points at them, but they make the new clone no longer clean. Patches avoid that.
Repairing an existing clone instead
If a collaborator has a large local setup, such as many branches or build caches, they can repair the clone in place. This takes more care and is easy to get wrong, so use it only when the user understands the steps.
git fetch --all --prune --tags --force
git switch main
git reset --hard origin/main
git reflog expire --expire=now --all
git gc --prune=nowThe --force on fetch is needed because tags were rewritten, and Git refuses to move an existing tag without it. The reset --hard discards local changes on main.
These commands fix only main. Every other local branch still points at old history, and so do stashes. Each local branch must be moved onto the new history with git rebase --onto, which names the new base, the old base, and the branch.
git rebase --onto origin/main OLD_BASE_HASH my-featureOLD_BASE_HASH is the old commit where the branch started, which you can find with git merge-base before you reset anything. If any step is unclear, a fresh clone is the safer choice.
Pull requests, forks, and CI
Open pull requests need attention because their commits changed.
- A pull request from a branch in the same repository needs its branch rebased onto the new
main. After a rebase, the author pushes withgit push --force-with-lease origin my-feature. This option refuses to overwrite remote work the author has not seen. - A pull request from a fork has the same problem, but you cannot fix the fork. The contributor must update their own fork. The earlier chapter on copies that survive covers the host's remaining references to old commits.
- CI jobs that cache the repository or pin a commit hash may fail. Clear their caches and update any pinned hashes.
Confirming a clone is clean
Anyone can check their new clone with the same search used during verification.
git log --all --oneline -S'AKIAEXAMPLEKEY12345'
git grep -I 'AKIAEXAMPLEKEY12345' $(git rev-list --all) || echo 'no matches'Empty output from the first command and the message from the second mean the clone holds no copy of the string in any ref. Run this check in the new clone before pushing any restored branch.
Handling common situations
| Situation | What to do |
|---|---|
| No local work | Delete the old clone and clone again. |
| Unpushed commits | Export with `git format-patch`, clone, apply with `git am --3way`. |
| Uncommitted changes | Save with `git diff`, apply with `git apply` in the new clone. |
| Open pull request | Rebase onto the new `main`, push with `--force-with-lease`. |
| Pull request from a fork | Contributor updates their fork, then pushes. |
| Someone already ran `git pull` | Do not push. Re-clone and carry over only their own commits. |
If someone has already pushed old history back, treat it as a new leak. The credential is already rotated, but you need to remove the reintroduced commits again with the same rewrite. Then ask that person to re-clone before they do anything else.
Exercise
A teammate pulled from the remote after your force-push, resolved the merge, and has not pushed yet. What should they do, and why is pushing the merge a problem?
Show answer
They should not push. The merge joins the old history, which contains the secret, to the rewritten history, and the push would be accepted as a normal fast-forward and put the old commits back on the remote. They should export their own unpushed commits with git format-patch, clone the repository again, and apply the patches with git am --3way.
Exercise
Why do collaborators need git fetch --tags --force if they repair a clone instead of re-cloning?
Show answer
The rewrite changed the commits that tags point to. Git refuses to move an existing local tag to a different commit unless you force it, so without --force the local tags would keep pointing at old history that contains the secret.