Skip to content

Instantly share code, notes, and snippets.

@gtklocker
Created April 16, 2023 08:38
Show Gist options
  • Save gtklocker/c94de1fca6bbfd033ddff9201f456b0d to your computer and use it in GitHub Desktop.
Save gtklocker/c94de1fca6bbfd033ddff9201f456b0d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
if __name__ == "__main__":
current_branch = subprocess.check_output(["git", "branch", "--show-current"]).rstrip()
if not current_branch:
raise Exception("need to be checked out to a named git branch")
merged_branches_with_upstream = subprocess.check_output(
["git", "branch", "--format", "%(refname:short) %(upstream:short)", "--merged"]
).split(b"\n")
for bu in merged_branches_with_upstream:
if not bu:
continue
(branch, upstream) = bu.split(b" ")
if branch == current_branch:
continue
print(f"deleting branch {branch}...")
subprocess.check_call(["git", "branch", "-d", branch])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment