Branches

Repository.branches

Branches inherit from References, and additionally provide specialized accessors for some unique features.

class pygit2.repository.Branches(repository, flag=3, commit=None)
__contains__(name)
__getitem__(name)
__iter__()
create(name, commit, force=False)
delete(name)
get(key)
with_commit(commit)

Example:

>>> # Listing all branches
>>> branches_list = list(repo.branches)
>>> # Local only
>>> local_branches = list(repo.branches.local)
>>> # Remote only
>>> remote_branches = list(repo.branches.remote)

>>> # Get a branch
>>> branch = repo.branches['master']
>>> other_branch = repo.branches['does-not-exist']  # Will raise a KeyError
>>> other_branch = repo.branches.get('does-not-exist')  # Returns None

>>> remote_branch = repo.branches.remote['upstream/feature']

>>> # Create a local branch
>>> new_branch = repo.branches.local.create('new-branch')

>>> And delete it
>>> repo.branches.delete('new-branch')

The Branch type

class pygit2.Branch

Branch.

branch_name

The name of the local or remote branch.

delete()

Delete this branch. It will no longer be valid!

is_checked_out()

True if branch is checked out by any repo connected to the current one, False otherwise.

is_head()

True if HEAD points at the branch, False otherwise.

raw_branch_name

The name of the local or remote branch (bytes).

remote_name

The name of the remote set to be the upstream of this branch.

rename(name, force=False)

Move/rename an existing local branch reference. The new branch name will be checked for validity. Returns the new branch.

upstream

The branch’s upstream branch or None if this branch does not have an upstream set. Set to None to unset the upstream configuration.

upstream_name

The name of the reference set to be the upstream of this one