PricingSign inDownload

Blog / git

Undo a rebase a coding agent ran

An agent rebased your branch and you want the old one back. Undo it with ORIG_HEAD or the reflog, keep commits it made after, or stop a rebase halfway.

On this page
  1. Right after the rebase: ORIG_HEAD
  2. Later on: find the old branch in the reflog
  3. Go back, and keep what came after
  4. See what the rebase changed first
  5. Stop a rebase halfway
  6. Already pushed
  7. Keep agents from rebasing
  8. Where Joxo fits
  9. Frequently asked questions
  10. How do I undo a git rebase?
  11. Can I undo a rebase after making more commits?
  12. How do I stop a rebase that's in progress?
  13. How long does git keep the old commits after a rebase?
  14. Try it with your team

Agents rebase to "update the branch", to tidy commits, or to get past a conflict. Sometimes the result isn't what you wanted: a conflict resolved the wrong way, commits dropped, or a branch your teammate had already pulled. The old branch is still in git. We ran every command with git 2.43 on a scratch repository.

First, stop the agent or let it finish. If it keeps committing, the steps below still work, but you'll have more to carry over.

Right after the rebase: ORIG_HEAD#

bash
git reset --hard ORIG_HEAD
HEAD is now at d7e3a2d Search by tag

A rebase saves where the branch was in ORIG_HEAD before it starts. Resetting to it puts the branch back exactly. It only works if nothing else has moved it since: a later reset, merge or another rebase overwrites ORIG_HEAD. Check git status --short first, because --hard also drops uncommitted edits.

Later on: find the old branch in the reflog#

bash
git reflog show search
09c59bb search@{0}: commit: Tidy imports
f6e7ef6 search@{1}: rebase (finish): refs/heads/search onto a409124192a3d4940ad4cec1b8097329ec59cd04
4144b17 search@{2}: commit: Search by tag
dc8f239 search@{3}: commit: Add search
f5aaf1c search@{4}: branch: Created from HEAD

This is every position the branch has had, newest first. The line under rebase (finish) is the branch before the rebase: here search@{2}, the agent's "Search by tag" commit. The line above it shows the agent kept working after the rebase and committed "Tidy imports".

Go back, and keep what came after#

Mark the current state so you can pick from it, reset to the old branch, then copy over the commits made after the rebase:

bash
git branch search-rebased
git reset --hard search@{2}
git cherry-pick search-rebased
HEAD is now at 4144b17 Search by tag
[search 138f7e7] Tidy imports
 1 file changed, 1 insertion(+)
 create mode 100644 z.js

The branch is back on its old base with the new commit on top. For several later commits, use a range from the rebase (finish) hash: git cherry-pick f6e7ef6..search-rebased. Use hashes rather than search@{…} numbers here, because the numbers shift each time the branch moves. When you're happy, git branch -D search-rebased.

Check the branch changes only what it should:

bash
git diff --stat main...search
 search.js | 2 ++
 z.js      | 1 +
 2 files changed, 3 insertions(+)

The three dots compare the branch with the point where it left main, so changes on main since then don't show up.

See what the rebase changed first#

Before you undo it, check what actually changed. Sometimes the rebase is fine and only the conflict needs a second look. git range-diff lines up the commits from before and after:

bash
git range-diff main ORIG_HEAD search
1:  05fcd52 < -:  ------- Add search
2:  62b7fa3 < -:  ------- Search by tag
-:  ------- > 1:  c6b1ec9 Add search

Lines with < are commits only in the old branch, lines with > only in the new one, and matching pairs show = (unchanged) or ! (changed, with the diff below). Here the branch had two commits and now has one: the agent resolved a conflict by putting everything in "Add search", and "Search by tag" disappeared. That's worth undoing. If you see only = lines, the rebase just moved the commits and you can keep it. Without ORIG_HEAD, use the hash of the pre-rebase line from the reflog.

Stop a rebase halfway#

If the agent stopped on a conflict, git status says so:

interactive rebase in progress; onto f021f4e
Last command done (1 command done):
   pick 86184e5 Add search

To throw the whole rebase away and return to the branch as it was:

bash
git rebase --abort

Don't let the agent run git rebase --skip to get past a conflict: it drops that commit from the branch. If it already has, the reflog steps above bring the commit back.

Already pushed#

A rebase gives the commits new hashes, so after undoing it the branch on the remote no longer matches yours. If only you and your agent use the branch, push the restored one:

bash
git push --force-with-lease origin search
 + 09c59bb...138f7e7 search -> search (forced update)

--force-with-lease refuses if someone else pushed in the meantime, where plain --force would overwrite their work. If a teammate has already pulled the rebased branch, talk to them before pushing over it.

Keep agents from rebasing#

Add to your project instructions: "Never rebase, force-push or run git reset --hard. To update a branch, merge main into it and stop if there's a conflict." A merge keeps every existing commit as it was, so there's nothing to undo, and you can still squash when the pull request lands.

Where Joxo fits#

A rebase is most painful when someone else's agent had already built on the old branch. Joxo shows each agent's task and branch on a shared board and passes a note when work changes hands, so agents know who else is on a branch before rewriting it. Setup is one line pasted into your agent.

Frequently asked questions#

How do I undo a git rebase?#

Right after the rebase, run git reset --hard ORIG_HEAD. Later, find the branch's position before the rebase with git reflog show <branch> and run git reset --hard <branch>@{n} with that number.

Can I undo a rebase after making more commits?#

Yes. Make a branch at the current commit, reset to the pre-rebase position from the reflog, then git cherry-pick the commits made after the rebase from that branch.

How do I stop a rebase that's in progress?#

Run git rebase --abort. The branch goes back to exactly where it was before the rebase started.

How long does git keep the old commits after a rebase?#

The reflog keeps them for 90 days by default, and commits not on any branch for at least 30, so you have weeks to undo a rebase.