Skip to content

Instantly share code, notes, and snippets.

@cuotos
Last active February 24, 2020 12:52
Show Gist options
  • Save cuotos/e868eac6c473e278df76cb8c0ddb7bfa to your computer and use it in GitHub Desktop.
Save cuotos/e868eac6c473e278df76cb8c0ddb7bfa to your computer and use it in GitHub Desktop.
find local git branches that do not exist on remote
#!/usr/bin/env python
import subprocess
try:
branches = subprocess.check_output(['git', 'branch', '-a']).splitlines()
except subprocess.CalledProcessError as e:
quit(e.returncode)
def filter_branches(branches):
branches = map(lambda l: l[1:].strip() if l.startswith("*") else l.strip(), branches)
branches = filter(lambda line: "remotes/origin/HEAD ->" not in line, branches)
return branches
def split_branches(branches):
local_branches = []
remote_branches = []
for b in branches:
if b.startswith("remotes/origin/"):
remote_branches.append(b[len("remotes/origin/"):])
else:
local_branches.append(b)
return local_branches, remote_branches
local, remote = split_branches(filter_branches(branches))
missing_branches = [b for b in local if b not in remote]
if len(missing_branches) > 0:
print("\n".join(missing_branches))
else:
print("no missing branches")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment