Skip to content

Instantly share code, notes, and snippets.

@sheepmaster
Created May 1, 2014 15:27
Show Gist options
  • Select an option

  • Save sheepmaster/d696bef676e72c493988 to your computer and use it in GitHub Desktop.

Select an option

Save sheepmaster/d696bef676e72c493988 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""Prints the last checked out branches (i.e. HEAD, @{-1}, @{-2}, ...), one per line.
Duplicates will be skipped, so the line number does not necessarily correspond to the |n| in `@{-n}`.
"""
import re
import subprocess
lines = subprocess.check_output(['git', 'reflog']).splitlines()
branches = set()
for line in lines:
match = re.match(r'^[0-9a-f]+ HEAD@\{\d+\}: '
r'checkout: moving from \S+ to (\S+)$', line)
if match:
branch = match.groups()[0]
if branch == 'HEAD':
continue
if not branch in branches:
branches.add(branch)
print branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment