A convenient git alias for cleaning up after merged pull requests.
git config --global alias.done '!f() { current_branch=$(git rev-parse --abbrev-ref HEAD); if [ "$current_branch" = "main" ]; then echo "Already on main branch"; git fetch && git pull && git remote prune origin; else git fetch && git checkout main && git pull && git branch -d "$current_branch" && git remote prune origin; fi; }; f'
After your PR is merged, simply run:
git done
- Fetch - Gets latest changes from origin
- Checkout main - Switches to main branch (if not already there)
- Pull - Updates local main with latest changes
- Delete branch - Removes the feature branch you were working on (with
-d
for safety) - Prune origin - Cleans up stale remote tracking branches
This alias works best when your GitHub repository is configured to automatically delete merged PR branches:
# Configure via GitHub CLI
gh api repos/OWNER/REPO --method PATCH --field delete_branch_on_merge=true
# Or enable via GitHub web UI:
# Settings → General → Pull Requests → "Automatically delete head branches"
With this setting enabled, the remote branch is deleted when you merge the PR, and git remote prune origin
cleans up the local tracking reference.
This alias works particularly well with rebase-based git workflows where:
- Feature branches are rebased onto main before merging
- PRs are merged using "Rebase and merge" or "Squash and merge" strategies
- Local feature branches can be safely deleted after merge since history is preserved in main
- Uses
git branch -d
(not-D
) to ensure branch is fully merged before deletion - Handles case where you're already on main branch
- Only deletes the current working branch, never main
Perfect for streamlining your git workflow after PRs are merged!