Skip to content

Instantly share code, notes, and snippets.

@CatherineH
Created March 1, 2017 19:09
Show Gist options
  • Save CatherineH/c8459d9292b17e394efd9cf714c3703b to your computer and use it in GitHub Desktop.
Save CatherineH/c8459d9292b17e394efd9cf714c3703b to your computer and use it in GitHub Desktop.
Python script to checkout the latest commit across all remote branches
from commands import getstatusoutput
def branches(remote=False):
command = "git branch" + (" -r" if remote else "")
return [br.strip() for br in getstatusoutput(command)[1].replace("*", "").split("\n")]
local_branches = branches()
remote_branches = branches(remote=True)
missing_branches = []
for branch in remote_branches:
_branch = branch.replace("origin/", "")
if _branch not in local_branches and _branch.find(" -> ") == -1:
missing_branches.append(_branch)
# git fetch
_ = getstatusoutput("git fetch")
for branch in missing_branches:
command = "git checkout "+branch
resp = getstatusoutput(command)
# git pull --all
_ = getstatusoutput("git pull --all")
# checkout latest version
output = getstatusoutput("git checkout $(git log --branches -1 --pretty=format:\"%H\")")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment