Skip to content

Instantly share code, notes, and snippets.

@RefusesNames
Last active June 15, 2024 10:07
Show Gist options
  • Select an option

  • Save RefusesNames/af3d5a45bef73b8d97e1000dc0e5b44f to your computer and use it in GitHub Desktop.

Select an option

Save RefusesNames/af3d5a45bef73b8d97e1000dc0e5b44f to your computer and use it in GitHub Desktop.
This function allows to delete local branches that have already been merge to (local) main. To ensure that no important branches are deleted, the names are written to a temporary file that can be edited via nvim. The branches remaining in that file are deleted (except for main)
function delete-branches {
      git switch main
      git branch --merged > tmp.txt
      nvim tmp.txt
      Get-Content tmp.txt | ForEach-Object {
            $to_delete = $_.Trim()
            if ($to_delete -ne "* main" -and $to_delete -ne "main") {
                  echo "Deleting $($to_delete)"
                  git branch -d $to_delete
            }
      }
      git switch -
      rm tmp.txt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment