Branches

Use ProjectBranch objects to manipulate repository branches.

To create ProjectBranch objects use the gitlab.Gitlab.project_branches or Project.branches managers.

Examples

Get the list of branches for a repository:

branches = gl.project_branches.list(project_id=1)
# or
branches = project.branches.list()

Get a single repository branch:

branch = gl.project_branches.get(project_id=1, id='master')
# or
branch = project.branches.get('master')

Create a repository branch:

branch = gl.project_branches.create({'branch_name': 'feature1',
                                     'ref': 'master'},
                                    project_id=1)
# or
branch = project.branches.create({'branch_name': 'feature1',
                                  'ref': 'master'})

Delete a repository branch:

gl.project_branches.delete(project_id=1, id='feature1')
# or
project.branches.delete('feature1')
# or
branch.delete()

Protect/unprotect a repository branch:

branch.protect()
branch.unprotect()

Note

By default, developers will not be able to push or merge into protected branches. This can be changed by passing developers_can_push or developers_can_merge like so: branch.protect(developers_can_push=False, developers_can_merge=True)