Skip to content
mentoor

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?

Choose one answer

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

  1. Branch first. git switch -c agent/fix-tax-rounding before the agent starts. Name it so others know it is agent work in progress.
  2. Undo by situation.
    • Change not committed: git restore path/to/file discards edits to one file. It cannot be recovered afterwards, so check git diff first.
    • 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.
  3. 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.
  4. Take a safety branch before risky work. git branch backup/before-agent costs 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 1 to pick the mainline parent; without it Git refuses.
  • Destructive cleanup. git clean -fd deletes 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 revert for commits that are pushed or shared, because it adds a commit and keeps history intact.
  • Use git reset --hard only on local commits nobody else has, and remember git reflog as a last resort.
  • Never force-push a shared branch; if you must force, use --force-with-lease on 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 main because the task is small.
  • Using git reset --hard on commits that were already pushed.
  • Force-pushing with plain --force instead of --force-with-lease.
  • Running git clean -fd or git restore . before checking what it will delete.

Check yourself · 2 of 3

Why start each agent task on its own branch?

Choose one answer

Check yourself · 3 of 3

You have two local commits on a branch you never pushed, and both are wrong. Which command is acceptable?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic