Removing Data from Source Control
This lesson explains why a delete commit is not enough, and how to remove data from history.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why data persists in history.
- State the correct order of actions for a leaked secret.
- Use git filter-repo to rewrite history.
- List the steps around a history rewrite.
Why deleting a file is not enough
If you delete a file and commit, the file disappears from the latest version, but every earlier commit still contains it. Anyone with a clone can retrieve it with a single command. Removing data truly requires rewriting all commits that contain it, which gives those commits new IDs.
The first response to a leaked credential is not a Git command. Revoke and rotate the credential first, because you must assume that it has been copied the moment it reached a shared repository. Cleaning history then removes a liability, and it does not undo the exposure. This order is one of the most reliable exam points in this area.
Deleting is not removing
Rewriting history with git filter-repo
git filter-repo is the tool recommended by the Git project, and it replaces the older git filter-branch, which is slow and error-prone. It is not installed with Git, so it must be added separately. It works best on a fresh clone, and it removes the origin remote by default as a safety measure, so that you do not push by accident.
# Work in a fresh clone
git clone https://github.com/contoso/web.git
cd web
# Remove one file from all history
git filter-repo --path config/secrets.json --invert-paths
# Remove all files larger than 50 MB
git filter-repo --strip-blobs-bigger-than 50M
# Replace a string everywhere; each line in replacements.txt
# has the form old-value==>REMOVED
git filter-repo --replace-text replacements.txt
# Push the rewritten history
git remote add origin https://github.com/contoso/web.git
git push origin --force --all
git push origin --force --tagsThe BFG Repo-Cleaner is a popular alternative that handles the same common tasks with a simpler interface, such as bfg --delete-files secrets.json.
git filter-repo options
Completing the removal
Rewriting a repository is only part of the task. The following sequence covers what must happen around it.
-
Rotate the secret
Revoke the exposed credential and issue a new one.
-
Announce a freeze
Ask contributors to merge or stash work, since rewriting conflicts with open branches.
-
Rewrite in a fresh clone
Run git filter-repo, and check the result.
-
Allow the force push
Temporarily permit force pushes, which branch policies and protection normally block.
-
Push all branches and tags
Replace history on the remote.
-
Replace clones
Everyone discards old clones and clones again, since old clones would reintroduce the data if pushed.
-
Clear remaining copies
Forks, cached pull request views and old pipeline logs may still hold the data. GitHub Support can purge cached views.
The following diagram shows the order of response to a storage account key committed to a repository six commits ago.

Removing a secret for good
Removing data in Azure Repos and preventing recurrence
In Azure Repos, a force push needs the Force push permission, and bypass of branch policies if the branch is protected. Azure DevOps runs garbage collection on unreachable objects, and removing the data fully may require contacting support. To remove large accidentally committed objects, rewriting with git filter-repo follows the same approach.
Prevention is better than repair. .gitignore keeps local configuration files out of commits, pre-commit hooks can detect secrets on a developer's machine, and push protection blocks known secret patterns before they reach the repository.
Prevent or repair?
A committed key
A developer committed a storage account key to a GitHub repository six commits ago. The commit is on main. The security team asks what to do first.
The first action is to rotate the storage account key, because the secret must be treated as compromised. Then use git filter-repo to remove it from history, force push, and have contributors clone again. A new commit that deletes the key would leave it in history. A revert would add a new commit in the same way. Making the repository private does not protect against people who have already seen or cloned it.
After rewriting history, why must everyone clone again?
Show answer
Old clones hold the removed data, and pushing from one could reintroduce it.
Exam Tips
Remember for the exam
- Rotate an exposed secret before you touch history.
- A delete commit leaves the data in earlier commits.
- git filter-repo is the recommended tool; filter-branch is discouraged.
- A rewrite needs a force push, and everyone must clone again.
- Old clones can reintroduce removed data if they are pushed.