Here is one approach:
- Use the GitHub API to get the HEAD SHA for all queries with merged pulls.
- Map these SHAs to local branches.
If the local HEAD branch corresponds to the volume of the combined PR, you can safely delete it. This will work regardless of how the PR has been combined (full merge, quick capture, squash and merge).
I implemented this approach in a delete-compressed branches script. Here what usage is as follows:
(master) $ git branch delete-merged-prs fixup unmerged-branch * master (master) $ delete-squashed-branches Finding local branches which were merged onto origin/master via GitHub... warning: deleting branch 'delete-merged-prs' that has been merged to 'refs/remotes/origin/delete-merged-prs', but not yet merged to HEAD. Deleted branch delete-merged-prs (was 325a42b). warning: deleting branch 'fixup' that has been merged to 'refs/remotes/origin/fixup', but not yet merged to HEAD. Deleted branch fixup (was e54f54b). (master) $ git branch unmerged-branch * master
(Warnings can be ignored since the branches were merged with the master through Squash and Merge.)
Cautions:
- You need
pip install pygithub use the script. - It will look for local branches that have been merged onto
origin/(current branch) . Thus, you will want to run it on master , develop or any other branch you are working from. - If you have multiple branches with a long lifetime, this approach will not work very well.
danvk source share