Skip to content

Instantly share code, notes, and snippets.

@membrive
Created August 4, 2020 08:56
Show Gist options
  • Save membrive/724ff26408127b3bace74ed08814843e to your computer and use it in GitHub Desktop.
Save membrive/724ff26408127b3bace74ed08814843e to your computer and use it in GitHub Desktop.
Get the name of the current git branch using git binary and no other Python libraries
#!/usr/bin/env python3
from subprocess import run, PIPE
branch_proc = run(['git', 'branch', '--show-current'],
stdout=PIPE,
universal_newlines=True,
cwd='.') # set CWD to the branch directory
if branch_proc.returncode == 0:
branch = branch_proc.stdout.strip('\n')
print(branch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment