Lesson 6 of 6 Repository Management

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.

True or false

Deleting is not removing

Exam skills 2.2.6

Check what happens when sensitive data is deleted from a repository.

Log in to try this activity Interactive activities are free with an account.

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 --tags

The BFG Repo-Cleaner is a popular alternative that handles the same common tasks with a simpler interface, such as bfg --delete-files secrets.json.

Match the pairs

git filter-repo options

Exam skills 2.2.6

Match each removal task with the git filter-repo option that performs it.

Log in to try this activity Interactive activities are free with an account.

Completing the removal

Rewriting a repository is only part of the task. The following sequence covers what must happen around it.

  1. Rotate the secret

    Revoke the exposed credential and issue a new one.

  2. Announce a freeze

    Ask contributors to merge or stash work, since rewriting conflicts with open branches.

  3. Rewrite in a fresh clone

    Run git filter-repo, and check the result.

  4. Allow the force push

    Temporarily permit force pushes, which branch policies and protection normally block.

  5. Push all branches and tags

    Replace history on the remote.

  6. Replace clones

    Everyone discards old clones and clones again, since old clones would reintroduce the data if pushed.

  7. 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.

The order of response to a storage account key committed six commits ago. Rotate the key first, because it must be treated as compromised; then remove it from history with git filter-repo, force push, and have contributors clone again. A deleting commit or a revert leaves it in history.
The scenario is illustrative.
Put in order

Removing a secret for good

Exam skills 2.2.6

Arrange the full procedure for removing a leaked secret from a shared repository.

Log in to try this activity Interactive activities are free with an account.

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.

Sort into groups

Prevent or repair?

Exam skills 2.2.6 · 4.3.3

Decide whether each measure prevents data reaching the repository or repairs it afterwards.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.