Sub topic 13
Telling collaborators to re-clone
This chapter assumes you have already rewritten the history and force-pushed it. It also assumes your collaborators have local clones of the repository. You should know git fetch, git merge, and git format-patch at a basic level.
Why old clones are a problem
A force-push changes the remote. It does not change anyone's local copy. Each collaborator still has the old commits, with the secret in them, in their .git directory.
Rewritten commits have new hashes. Git sees them as unrelated to the old ones. If a collaborator runs git pull from an old clone, Git tries to merge the two histories. The result is a merge commit whose parents include the old, secret-bearing commits. Pushing that merge puts the secret back on the remote.
An old clone can leak the secret in two ways:
- Pulling and merging: the old history is combined with the new history.
- Pushing old branches: a stale local branch is pushed, and its commits reappear on the remote.
The safe default: re-clone
Tell everyone to make a fresh clone, then delete the old one. A fresh clone only contains the rewritten history. Rebasing or resetting an old clone is not enough, because the old objects stay in .git until garbage collection removes them. They can also be restored from the reflog.
Message to send
Send the message after the force-push. Keep it short and give exact steps.
The history of org/repo was rewritten to remove a leaked credential.
Do not run git pull, git merge, or git push from your existing clone.
1. Push or save any unpushed work (see below).
2. Clone the repository again into a new directory.
3. Delete your old clone.Don't put the secret in the message. Don't name the file or commit that contained it unless that is already public.
Saving unpushed work
Collaborators may have local commits that never reached the remote. Don't copy them over with git fetch from the old clone. A fetch would bring the old objects, secret included, into the new clone.
Export the commits as patches instead. Patches contain only the changes, not the old history.
# In the OLD clone: export commits on my-feature that are not on origin/main
git format-patch origin/main..my-feature -o ../patches
# In the NEW clone: recreate the branch and apply the patches
git switch -c my-feature origin/main
git am ../patches/*.patchThe git am step can fail if a patch touches a file that the rewrite changed or removed. Resolve the conflict, then run git am --continue. Before pushing, search the patch files for the secret. A patch that adds the secret again would undo your cleanup.
grep -rl 'AKIAEXAMPLEKEY12345' ../patches || echo 'patches are clean'The collaborator workflow
The diagram shows the decision each collaborator makes.
Confirming everyone has moved
You can't see other people's local clones. Ask each collaborator to confirm in the team channel, and keep a checklist. Also check your CI runners and build servers. They often hold long-lived clones that nobody thinks about. Clear their caches or recreate their workspaces.
Exercise
A teammate ran git pull in their old clone after your force-push and now has a merge commit. They have not pushed yet. What should they do, and why?
Show answer
They should not push. They should export any real work they did with git format-patch, then delete the clone and re-clone. The merge commit has the old secret-bearing commits as a parent, so pushing it would put the secret back on the remote.