Blog / git
Undo a merge a coding agent made
An agent merged a branch you weren't ready for. Undo it before you push, stop a merge stuck on a conflict, or revert it once it's on main.
On this page
- Look at what was merged
- Check how it resolved conflicts
- The merge stopped on a conflict
- Not pushed yet: reset to before the merge
- Already pushed: revert it
- Merging the branch again after a revert
- Keep agents from merging on their own
- Where Joxo fits
- Frequently asked questions
- How do I undo a git merge that hasn't been pushed?
- How do I undo a merge that's already pushed?
- How do I cancel a merge with conflicts?
- Why does git say "Already up to date" when I merge a reverted branch again?
- How do I see how a merge's conflicts were resolved?
- Try it with your team
You asked an agent to "get the latest changes in" or "finish the feature", and it merged a branch into main: too early, the wrong branch, or with a conflict resolved the wrong way. How you undo it depends on whether the merge finished and whether it has been pushed. We ran every command with git 2.43 on a scratch repository.
Look at what was merged#
git log --oneline --graph -6* 601b44b Merge branch 'search'
|\
| * f785512 Search by tag
| * c57a0ba Add search
* | 2c4b461 Fix header
|/
* e0208e5 Initial commitThe merge commit has two parents: the line on the left is main as it was, the one on the right is the branch that came in. To see every file the merge changed on main:
git diff --stat HEAD^1 HEAD search.js | 2 ++
1 file changed, 2 insertions(+)HEAD^1 is the first parent, main before the merge.
Check how it resolved conflicts#
If the merge had conflicts, the agent chose how to settle them, and an ordinary diff doesn't show that choice. --remerge-diff redoes the merge, conflicts included, and diffs it against what the agent committed:
git show --remerge-diff --format='%h %s' HEAD26af09d Merge branch 'other'
diff --git a/app.js b/app.js
remerge CONFLICT (content): Merge conflict in app.js
index b82c353..f13a556 100644
--- a/app.js
+++ b/app.js
@@ -1,5 +1 @@
-<<<<<<< 4a3c999 (Shout title)
-TITLE
-=======
Title
->>>>>>> ab6061b (Capitalise title)Read it as "the conflict git saw, and what the agent kept". Here main had TITLE, the branch had Title, and the agent threw away main's side entirely. If that's wrong, undo the merge below and resolve it yourself, or keep the merge and fix the file in a new commit. A merge with no conflicts shows nothing here, which is also worth knowing.
The merge stopped on a conflict#
If the agent hit a conflict and stopped, nothing has been committed yet. git status says so:
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)To put everything back as it was before the merge started:
git merge --abortThe branch and its files return to the last commit. If you had uncommitted edits when the merge started, git can't always restore them exactly, so commit your own work before letting an agent merge.
Not pushed yet: reset to before the merge#
Check that the merge is only on your machine:
git status -sb## main...origin/main [ahead 2]ahead means you have commits the remote doesn't, here the merge and the commit it brought in. Then go back:
git reset --merge ORIG_HEADgit log --oneline -22c4b461 Fix header
e0208e5 Initial commitA merge saves where the branch was in ORIG_, and --merge resets to it while keeping uncommitted edits that the merge didn't touch, where --hard would drop them. The same works for a fast-forward merge, which has no merge commit to point at. If something else has moved the branch since, find the line before the merge with git reflog and reset to its hash instead.
The merged branch itself is untouched, so you can merge it again later when it's ready.
Already pushed: revert it#
Once the merge is on a shared main, don't rewrite it. Add a commit that undoes it:
git revert -m 1 --no-edit HEAD[main 696e84b] Revert "Merge branch 'search'"
1 file changed, 2 deletions(-)
delete mode 100644 search.js-m 1 tells git to keep the first parent, main, and undo everything the other side brought in. Use the merge's hash in place of HEAD if more commits came after it. Push as usual: everyone who pulls gets the undo, and nobody has to deal with rewritten history.
Merging the branch again after a revert#
This one catches people out. Once the branch is fixed, merging it again does nothing:
git merge --no-edit searchAlready up to date.Git sees the branch's commits as already merged; the revert only undid their changes. To bring them back, revert the revert:
git revert --no-edit HEAD[main cb5d04c] Reapply "Merge branch 'search'"
1 file changed, 2 insertions(+)
create mode 100644 search.jsThen merge the branch again to pick up any fixes made on it since. If the revert isn't the last commit, give its hash.
Keep agents from merging on their own#
Add to your project instructions: "Never merge into main or push to it. Work on your own branch and tell me when it's ready." Then the only merges are the ones you make, usually through a pull request. To make it stick, a branch protection rule on your host can refuse pushes to main that don't come through a pull request (see stop a coding agent committing to main).
Where Joxo fits#
An early merge often happens because an agent didn't know a branch was still in progress. Joxo puts each agent's task on a shared board with its status, so an agent can see that a teammate's branch isn't finished before it pulls it in. Setup is one line pasted into your agent.
Frequently asked questions#
How do I undo a git merge that hasn't been pushed?#
Run git reset --merge ORIG_HEAD right after the merge. If the branch has moved since, find the commit before the merge with git reflog and reset to that hash.
How do I undo a merge that's already pushed?#
Run git revert -m 1 <merge hash>. It adds a commit that removes everything the merge brought in and keeps the history as it is, so it's safe on a shared branch.
How do I cancel a merge with conflicts?#
Run git merge --abort. The branch goes back to the last commit, as if the merge never started.
Why does git say "Already up to date" when I merge a reverted branch again?#
The branch's commits are still in the history; the revert only undid their changes. Revert the revert commit first, then merge the branch again.
How do I see how a merge's conflicts were resolved?#
Run git show --remerge-diff <merge hash>. Git redoes the merge with its conflict markers and shows the difference from what was committed, so you can see which side was kept in each conflict.