Day 2 - Git recovery
This is Day 2 of DevOps Engineer Mastery. Practise on a throwaway repository you create yourself. Do not force-push shared main. Do not practise destructive Git on a work remote.
Happy path first
Worktree = files you edit. Index/staging = the next snapshot. Commit = snapshot + parent + SHA. The repo holds objects and names.
worktree -git add→ index -git commit→ commit ← branch name
↑
HEAD
Do this before any recovery:
- Create a file
git statusgit addgit commit- Change the file
git log- Make a branch
- Look at HEAD
- Draw reachable vs unreachable
If you cannot draw that, stop. Recovery will just be muscle memory.
Three different undos
They are not synonyms.
- reset moves the branch name / HEAD.
--hardalso wipes the worktree. Throwaway only. - revert makes a new commit that reverses an old one. This is what you do on shared
main. - restore puts file bytes back. It does not undelete a branch name.
- reflog is this clone's "where HEAD was." It is local.
Fetch vs pull: pulling is fetch plus integrate. A local revert is not "the team has it" until it is pushed.
Locked practice
On a throwaway repo:
- Happy path above.
reset --hardon a private branch. Predict HEAD before you run it. Then check.- Revert a middle commit (two good commits on top of a bad one). Push the revert, not a force.
- Delete a branch name. Restore it from reflog. Notice that restore-the-file is a different verb.
Production
No destructive Git on shared or work remotes. Never reset --hard origin/main && push --force as a first answer on shared main.
AI review (reject this)
reset --hard origin/main && push --force as the opening move when twelve people already pulled main.
Interview kill
Two good commits on top of the bad one. "I reverted so the team is fixed" is false until they have the revert commit.
Definition of done
You can explain reset vs revert vs restore. You predicted HEAD before a reset. You recovered a deleted branch from reflog. You did not force-push main.