Topic 4 of 5 · 5 min read
Recovering from destructive git operations
Goal: Recover lost commits after an agent's hard reset or force push using the reflog or remote refs, and name the protection that prevents a repeat.
Git rarely deletes committed work at once: after a hard reset, a bad rebase or a force push, the commits usually still exist, and the reflog or a remote ref tells you where. Stay calm, do not run more commands that rewrite things, and find the old tip.
Check yourself · 1 of 3
After an agent runs git reset --hard, where do you look first for the lost commits?
Why it matters
An agent told to "clean up" may run git reset --hard or git push --force. Commits that were on a branch become unreachable, and the branch looks as if the work never existed. But Git keeps unreachable commits until garbage collection removes them, which by default takes weeks for commits recorded in the reflog (ninety days by default for reachable entries, thirty for unreachable ones).
How to do it
- Stop and do nothing else. Do not run
git gcorgit reflog expire. - Read the reflog. It records every move of HEAD and branch tips in your local clone.
git reflog
# 9d41e7c HEAD@{0}: reset: moving to origin/main
# c83b1f2 HEAD@{1}: commit: Add CSV export tests
# a07d5e9 HEAD@{2}: commit: Add CSV export
- Put a branch on the lost commit before anything else:
git branch rescue/csv-export c83b1f2
- After a force push, the remote lost the commits but other clones may have them. Look at
git reflog show origin/main, an older clone, a CI checkout or a hosting platform's activity log. Push the rescue branch back withgit push origin rescue/csv-export. - Uncommitted work is different. A hard reset on a dirty tree destroys uncommitted changes, and the reflog cannot restore them. This is why agents should commit early.
Prevention
- Protect shared branches. Branch protection or rulesets on main block force pushes and deletions. See the GitHub document linked below.
- Use
--force-with-leaseinstead of--force; it refuses if the remote has commits you have not seen. - Give agents narrow permissions and keep a mirror or backup of the central repository.
- Tell agents plainly in their instructions: no
reset --hard, no force push, noclean -fdwithout asking.
Before and after
Before: an agent force-pushes and main now lacks two days of work. The team panics and starts re-doing it.
After: someone finds the old tip in a colleague's clone with git log origin/main, pushes it to a rescue branch, and merges. With protection on, the push would have been rejected.
What goes wrong
- Searching too late. Reflog entries expire and fresh clones have no history of the old tip.
- Fixing on top of the mess. More resets overwrite the clues.
- Assuming the reflog is shared. It is local to one clone, so recover on the machine where the agent ran.
Examples
Undoing an agent's hard reset
git reflog -5
git branch rescue/lost c83b1f2
git switch agent/codex/csv-export
git reset --hard c83b1f2
The branch tip moves back to the lost commit. The rescue branch stays as a bookmark until you are sure.
A protected main
With branch protection on, a force push to main is rejected by the server with a message that the branch is protected. The agent's command fails and nothing is lost. Turn on the rule that blocks force pushes and deletions, and require pull requests.
Key points
- Committed work survives a hard reset or force push as unreachable commits, and the reflog shows where they were.
- Put a rescue branch on the lost commit first, then decide how to repair.
- After a force push, look for the old tip in other clones, CI checkouts and the host's activity log.
- Uncommitted changes destroyed by reset --hard are not recoverable, so commit early.
- Branch protection, --force-with-lease and narrow agent permissions prevent a repeat.
Common mistakes
- Running more resets or git gc before locating the lost commit.
- Expecting the reflog of one clone to exist on another machine or on the server.
- Believing uncommitted changes can be recovered after reset --hard.
- Using plain --force instead of --force-with-lease, or leaving main unprotected.
Check yourself · 2 of 3
Which setting most directly stops an agent from force-pushing over main?
Check yourself · 3 of 3
An agent ran git reset --hard on a folder holding edits that were never committed. What is true?
Practice items, not counted towards your mastery.
Start here
- Pro Git book (opens in a new tab)BookFree
The free reference book for branching, rebasing, reset and recovery, written by Git's maintainers' community.
- git-worktree documentation (opens in a new tab)DocsFree
How to give each task its own checkout of one repository, and how to list, lock and remove them.
- git-bisect documentation (opens in a new tab)DocsFree
Exact syntax for bisect start, good, bad, run and reset, with exit-code rules for scripts.
More for this topic
- git-reflog (Git documentation) (opens in a new tab)DocsFree
How the reflog records where branch tips have been, and how to expire entries.
- About protected branches (GitHub Docs) (opens in a new tab)DocsFree
The rules that block force pushes, deletions and unreviewed merges on a shared branch.
- git-push documentation (opens in a new tab)DocsFree
Explains rejected pushes, --force-with-lease and why a plain force push is dangerous.