/cleanup-git
After merging a PR, this command finds the most recently merged branch using the GitHub CLI, confirms with the user, then checks out main, pulls with prune, and deletes the merged branch.
-
Run the following to get the most recently merged PR's branch name:
gh pr list --state merged --limit 1 --json headRefName --jq '.[0].headRefName' -
Show the user the branch name and ask: "Found recently merged branch:
{branch}. Run cleanup? (checkout main, pull --prune, delete branch)" -
Wait for explicit confirmation before proceeding.
-
If confirmed, run these commands in sequence:
git checkout main git pull --prune git branch -D {branch} -
Report the result of each command. If any command fails, stop and report the error without running subsequent commands.
- Requires
ghCLI to be installed and authenticated - The branch deletion uses
-D(force delete) since squash-merged branches won't appear as fully merged to git - This only looks at merged PRs on the remote, not local branch state