# Rebase can leave REBASE_HEAD and AUTO_MERGE behind; Phillip Wood has a fix

_If the last pick of a rebase conflicts, both refs survive after the rebase finishes._

Section: Bugs. Edition of 2026-10-28. Written by an AI editor from the thread "[PATCH 0/3] sequencer: fix cleanup of REBASE_HEAD and AUTO_MERGE" (7 messages): https://gitlist.dev/t/66678

Phillip Wood started from a bug report that REBASE_HEAD was still present after a rebase had finished. He then found that the same could happen to AUTO_MERGE, the ref the merge machinery creates for comparing a conflict resolution with the conflicted result. He credits Long for the report and Matt for the confirmation.

The sequencer deleted REBASE_HEAD at the start of each iteration of the commit-picking loop, but not after the loop ended. With conflicts on the last commit picked, the ref remained. Wood's fix deletes it in do_commit() when the commit succeeds, and before the loop in pick_commits(), so it is removed when continuing after conflict resolution or a failed commit. AUTO_MERGE is removed once before the loop instead of on every iteration, since it is only written when the rebase stops for conflicts.

A third patch changes test_ref_missing() to print the name of the ref that wrongly exists, so a failing test with several calls is easier to understand.

Junio C Hamano called the observation nice and the patch surprisingly short. He described the second patch as dense, which he blamed on the overly long functions and deep call chain. He explained the logic: the existing removal in the loop may not run when the user resumes with an exhausted todo list, so removal at the start of pick_commits() and in do_commit() covers that case. He found it made sense and approved of the test helper change, noting that test_ref_exists is defined immediately before it. His overall verdict was "very good".
