Design and implement a source control strategy · Topic 3 of 3

Recovering and Removing Data with Git

AZ-400 Git recovery: reflog, reset, revert and restore, plus removing leaked secrets and large files from history with filter-repo and BFG.

  • 5 min
In a nutshell
  • git revert adds an undo commit and is safe for shared history. git reset moves a branch and rewrites history.
  • git reflog finds commits after a reset or a branch deletion.
  • Deleting a file in a new commit does not remove it from history. Use git filter-repo or BFG, then force push.

What it is

Two opposite jobs come up in source control. Recovering data means bringing back something that was lost or changed: a deleted branch, a bad merge, an old file version. Removing data means making something disappear from history for good, such as a leaked secret or a huge binary.

They use different tools. Recovery uses commands that add history or move pointers. Removal has to rewrite history, which changes every commit after the change.

Mistakelost work or leaked secretRecoverreflog, revert, restoreRemovefilter repo, force pushRotate and re clonesecrets and teammates

How it works

Undoing and recovering

GoalCommandNotes
Undo a commit that is already sharedgit revert <commit>Adds a new commit that reverses the change. Safe on shared branches because history is not rewritten
Move a branch back and discard later commitsgit reset --hard <commit>Rewrites the branch pointer. Fine locally, dangerous on shared branches
Keep the changes but undo the commitgit reset --soft <commit> or --mixed--soft keeps changes staged, --mixed unstages them
Restore one file from a commitgit restore --source=<commit> <path>Older form: git checkout <commit> -- <path>
Discard uncommitted edits to a filegit restore <path>Cannot be undone
Bring one commit onto another branchgit cherry-pick <commit>Copies the change as a new commit
Park unfinished workgit stash and git stash popSaves local changes without committing
Find lost commitsgit reflogLists where HEAD and branches have been, even after a reset or a deleted branch

Recovering a deleted branch or lost commit

Git rarely loses a commit right away. If you deleted a branch locally, look it up in the reflog and recreate it.

git reflog
git branch rescued-work 4f2a9c1

On the server side, Azure Repos lets you search for a deleted branch on the Branches page and restore it. GitHub shows a Restore branch button on a closed pull request. Both depend on the commits still existing on the server.

Removing specific data

A new commit that deletes a file does not remove it. Anyone can still read it from older commits. To purge data you rewrite history.

ToolUse
git filter-repoThe recommended tool for rewriting history, for example removing a file or path everywhere (git filter-repo --path secrets.env --invert-paths) or replacing text
BFG Repo-CleanerA simpler, fast tool for common cases such as deleting large files or replacing passwords
git filter-branchThe older built in command. Slow and discouraged
git rm --cached <file>Stops tracking a file going forward. It does not touch history

The process after a secret leaks:

  1. Revoke and rotate the secret first. Treat it as compromised the moment it was pushed.
  2. Rewrite history with git filter-repo or BFG on a fresh mirror clone.
  3. Force push the cleaned branches and tags (git push --force --all, then --tags).
  4. Ask every collaborator to re-clone, because their copies still contain the old commits.
  5. Remove cached copies: close old pull requests and, on GitHub, contact support for cached views if needed. Run git gc after expiring the reflog locally.
  6. Add the file pattern to .gitignore and turn on secret scanning with push protection so it cannot happen again.

Choosing revert, reset or rewrite

SituationUse
Bad commit already merged to a shared branchgit revert
Local commits not yet pushed need to gogit reset
Deleted a branch by accidentgit reflog then git branch
Password committed and pushedRotate, then git filter-repo, then force push
Repository is bloated by an old binarygit filter-repo or BFG, then LFS going forward

A worked scenario

A developer pushes a storage account key to main. The security lead has the key rotated within minutes, which is the step that actually stops misuse. The team then creates a mirror clone, runs git filter-repo --path config/prod.env --invert-paths, force pushes, and asks everyone to re-clone. They add *.env to .gitignore and enable push protection, which blocks pushes that contain recognised secrets.

What to remember for the exam

  • git revert adds an undo commit and is safe for shared history. git reset moves a branch and rewrites history.
  • git reflog finds commits after a reset or a branch deletion.
  • Deleting a file in a new commit does not remove it from history. Use git filter-repo or BFG, then force push.
  • Rotate leaked secrets first, then clean history. Collaborators must re-clone.
  • Secret scanning with push protection prevents the repeat.
Watch out

Rewriting history changes commit IDs. Open pull requests and every clone are affected, so coordinate it.

How the exam asks about this

  • "Undo a change on main without rewriting history." git revert.
  • "Recover a branch deleted locally." git reflog then recreate the branch.
  • "Remove a committed file from all history." git filter-repo or BFG, then force push.
  • "A password was pushed. What is the first step?" Rotate the password.

Common mistakes

Cleaning history but not rotating the secret. Copies and caches may already exist.

Using git reset --hard on a shared branch. It forces everyone else to repair their clones.

Forgetting tags. Tags can still point at old commits that hold the data.

Not telling the team. Old clones can push the removed data right back.

Check yourself

  • Which command safely undoes a pushed commit by adding a new one?

    git revert.

  • Which command lists previous positions of HEAD so a lost commit can be found?

    git reflog.

  • What must you do before cleaning a repository of a leaked key?

    Revoke and rotate the key.

Topic 3 of 3 in Design and implement a source control strategy

Test yourself on Design and implement a source control strategy

The full Microsoft AZ-400 question bank has 766 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Microsoft. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.