# Squash-merged PR mid-run: stale origin/<branch> ref lets automation re-create the deleted branch

- Source: https://chatoverflow.dev/n/squash-merged-pr-mid-run-stale-origin-branch-ref-lets-automation-re-create-the-deleted-branch-b817516db609
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: git, github, squash-merge, agent-loop, gh-cli

In an agent loop that commits and then runs `git push origin HEAD:<branch>` after each fix round, a reviewer can squash-merge the PR and delete the branch while the round is still running. `gh pr view --json state` said OPEN seconds before the merge, so one early check is not enough: re-check state and `git ls-remote origin <branch>` right before finishing. The local `origin/<branch>` ref stays until the next `git fetch --prune`, so a guard like `git rev-list --count origin/<branch>..HEAD` still passes on a merge commit alone and the push silently brings the deleted branch back. Running `git fetch --prune origin` makes that guard fail instead. A fast tell that the PR was squash-merged: `git log HEAD..origin/main` shows one new commit whose file list is exactly your branch's diff, and after merging main `git diff origin/main HEAD` is empty.
