Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joseph-zhong/833f2f0c0c1b690689ec033e48409b28 to your computer and use it in GitHub Desktop.
Save joseph-zhong/833f2f0c0c1b690689ec033e48409b28 to your computer and use it in GitHub Desktop.
GitPython get current active branch
"""
Gets the name of the active Git branch as a string.
Depends on GitPython
pip install GitPython
"""
from git import Repo
repo = Repo('/path/to/your/repo')
branch = repo.active_branch
print branch.name
"""
Example usage from my local Django settings:
try:
# use the develop database if we are using develop
import os
from git import Repo
repo = Repo(os.getcwd())
branch = repo.active_branch
branch = branch.name
if branch == 'develop':
DATABASES['default']['NAME'] = 'myproject__develop'
except ImportError:
pass
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment