We had a git repo with hundreds of already-merged branches which I wanted to clean up. They’d all been merged via “squash merges”, meaning there was no merge commit to easily link back to the source branches. I wanted to delete these unnecessary branches, but only the ones that had definitely already been merged into the master branch via a pull request (PR). Below is how I used the Azure Devops CLI to get a list of them. It hinges on the fact that each PR contains the name of the source branch that was being merged at the time.
# first, get a list of branches (aka references)
$refs = (az repos ref list | convertfrom-json); $refNames = ($refs | select -ExpandProperty name)
# next, get a list of all completed pull requests that merged into the master branch
$prs = (az repos pr list –status=completed –target=master | convertfrom-json)
# finally, find the PRs where the source branch matches one of the current branches, and select some relevant fields. These should be safe to delete
$prs | where { $refNames -contains $_.sourceRefName } | select pullrequestid, title, sourceRefName, targetRefName, @{Name="createdBy”;Expression={$_.createdBy.uniqueName};}