Topic 2 of 5 · 5 min read
Branches and reverts as safety nets
Goal: Choose between revert, reset and a new branch to undo a bad agent change without losing anyone else's work.
Give each agent task its own branch, and undo shared mistakes with git revert, which adds history, not with git reset, which erases it. The rule of thumb: rewrite only commits nobody else has.
Check yourself · 1 of 3
A bad agent commit is already merged into main. What is the safest way to undo it?
Why it matters
An agent can run for an hour and make 30 commits. If they land on main, a bad result means untangling your colleagues' work from the agent's. On a branch, the worst case is deleting the branch. The same machine often runs several agent sessions and several people, so a rewritten shared branch can silently destroy work that is not yours.
How to do it
- Branch first.
git switch -c agent/fix-tax-roundingbefore the agent starts. Name it so others know it is agent work in progress. - Undo by situation.
- Change not committed:
git restore path/to/filediscards edits to one file. It cannot be recovered afterwards, so checkgit difffirst. - Commits only on your local branch, not pushed:
git reset --hard <good-commit>is acceptable. The reflog (git reflog) usually still holds the old tip for a while. - Commits already pushed or merged:
git revert <commit>creates a new commit that undoes the change. History stays intact for everyone.
- Change not committed:
- Never force-push a shared branch. If you must update a branch only you use, prefer
git push --force-with-lease, which refuses when someone else has pushed. - Take a safety branch before risky work.
git branch backup/before-agentcosts nothing.
git switch -c agent/fix-tax-rounding
# ...agent works, commits...
git log --oneline -5
git revert 4f2a9c1 # bad commit already pushed
git reset --hard 7be03d2 # only if nothing was pushed
Before and after
Before: an agent hit a failing test, ran git reset --hard origin/main and git push --force, and two teammates' commits disappeared. After: the agent works on its own branch, force-push is on the forbidden list, and a bad merge is undone with one revert commit.
What goes wrong
- Reset on shared history. Collaborators now have commits your branch no longer contains.
- Revert of a merge needs
-m 1to pick the mainline parent; without it Git refuses. - Destructive cleanup.
git clean -fddeletes untracked files with no recovery.
Examples
Undoing a merged bad change
A discount change merged on Tuesday and prices are wrong. Run git revert -m 1 <merge-commit> on a new branch, open a PR, merge it. Everyone's clone stays valid, and the fix can come back later by reverting the revert.
The agent that 'cleaned up'
git fetch origin
git reset --hard origin/main
git push --force origin main
The agent wanted a clean tree. This sequence overwrites the remote main with whatever the agent's clone held and drops other people's pushes. A rule in the memory file and branch protection on main both stop it.
Key points
- Start every agent task on its own branch so the worst case is deleting the branch.
- Use
git revertfor commits that are pushed or shared, because it adds a commit and keeps history intact. - Use
git reset --hardonly on local commits nobody else has, and remembergit reflogas a last resort. - Never force-push a shared branch; if you must force, use
--force-with-leaseon a branch only you use. - Create a backup branch before an agent does risky or wide-ranging work.
Common mistakes
- Letting an agent commit straight to
mainbecause the task is small. - Using
git reset --hardon commits that were already pushed. - Force-pushing with plain
--forceinstead of--force-with-lease. - Running
git clean -fdorgit restore .before checking what it will delete.
Check yourself · 2 of 3
Why start each agent task on its own branch?
Check yourself · 3 of 3
You have two local commits on a branch you never pushed, and both are wrong. Which command is acceptable?
Practice items, not counted towards your mastery.
Start here
- Pro Git book (opens in a new tab)BookFree
The full reference for staging, branching, reset and revert, written by Git's maintainers and community.
- Conventional Commits (opens in a new tab)DocsFree
A short, widely used commit message format you can paste into an agent's rules.
- Push protection (GitHub Docs) (opens in a new tab)DocsFree
How GitHub blocks pushes that contain secrets, and what to do when a push is blocked.
More for this topic
- git-revert (Git documentation) (opens in a new tab)DocsFree
The official description of how revert adds a new commit that undoes an earlier one.