Retiring a long-lived branch on GitHub: switch the default, delete the old branch, and the checks worth doing first
Finished a merge where all work had lived on a branch that was also GitHub's default, and the target 'main' existed only locally (its upstream showed as 'gone').
Sequence that worked cleanly:
1. Confirm nothing is lost: git fetch origin --prune, then compare git rev-parse origin/main origin/<old>; they should be the same commit after the fast-forward and push.
2. Check for dependents before deleting: gh pr list --state open (open PRs targeting or coming from the old branch), and grep the repo's docs, scripts and CI config for the branch name. On a private repo on the free plan gh api repos/<owner>/<repo>/branches/<name>/protection returns 403 'Upgrade to GitHub Pro', which simply means there is no protection to worry about.
3. Switch the default first: gh repo edit <owner>/<repo> --default-branch main, and verify with gh repo view --json defaultBranchRef. GitHub refuses to delete the current default branch, so the order matters.
4. Delete the old branch remotely and locally: git push origin --delete <old>, git branch -d <old> (the lowercase -d is a free safety check that it is fully merged).
5. Fix the local idea of the remote's default: git remote set-head origin main, otherwise origin/HEAD keeps pointing at the deleted branch. git branch -a -vv should then show only main and origin/HEAD -> origin/main.
Notes:
- Changing the default branch and deleting a branch are owner-level settings; get an explicit yes for them separately from the merge itself.
- If an agent or tool keeps notes or config that name the old branch (memory files, harness settings that say which branch PRs target), update those too; the repo grep will not find them.
- gh api repos/<owner>/<repo>/branches -q '.[].name' is a quick final check of what exists on the remote.