---
title: Branch management
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import CodeBlock from "@theme/CodeBlock";
import import_branchList from '!!raw-loader!../../python-sdk/examples/branch_list.py';
import import_branchListSync from '!!raw-loader!../../python-sdk/examples/branch_list_sync.py';
import import_branchCreate from '!!raw-loader!../../python-sdk/examples/branch_create.py';
import import_branchCreateSync from '!!raw-loader!../../python-sdk/examples/branch_create_sync.py';
import import_branchRebase from '!!raw-loader!../../python-sdk/examples/branch_rebase.py';
import import_branchRebaseSync from '!!raw-loader!../../python-sdk/examples/branch_rebase_sync.py';
import import_branchMerge from '!!raw-loader!../../python-sdk/examples/branch_merge.py';
import import_branchMergeSync from '!!raw-loader!../../python-sdk/examples/branch_merge_sync.py';

# Branch management

The Python SDK provides multiple methods to manage the branches in an Infrahub instance.

## Get a single branch

<Tabs groupId="async-sync">
  <TabItem value="Async" default>

  ```python
  from infrahub_sdk import InfrahubClient
  client = InfrahubClient()
  branch = await client.branch.get(branch_name="main")
  ```

  </TabItem>
  <TabItem value="Sync" default>

  ```python
  from infrahub_sdk import InfrahubClientSync
  client = InfrahubClientSync()
  branch = client.branch.get(branch_name="main")
  ```

  </TabItem>
</Tabs>

## Get all the branches

<Tabs groupId="async-sync">
  <TabItem value="Async" default>
  <CodeBlock language="python">{import_branchList}</CodeBlock>
  </TabItem>
  <TabItem value="Sync" default>
  <CodeBlock language="python">{import_branchListSync}</CodeBlock>
  </TabItem>
</Tabs>

## Create a branch

<Tabs groupId="async-sync">
  <TabItem value="Async" default>
  <CodeBlock language="python">{import_branchCreate}</CodeBlock>
  </TabItem>
  <TabItem value="Sync" default>
  <CodeBlock language="python">{import_branchCreateSync}</CodeBlock>
  </TabItem>
</Tabs>

## Rebase a branch

<Tabs groupId="async-sync">
  <TabItem value="Async" default>
  <CodeBlock language="python">{import_branchRebase}</CodeBlock>
  </TabItem>
  <TabItem value="Sync" default>
  <CodeBlock language="python">{import_branchRebaseSync}</CodeBlock>
  </TabItem>
</Tabs>

## Merge a branch

<Tabs groupId="async-sync">
  <TabItem value="Async" default>
  <CodeBlock language="python">{import_branchMerge}</CodeBlock>
  </TabItem>
  <TabItem value="Sync" default>
  <CodeBlock language="python">{import_branchMergeSync}</CodeBlock>
  </TabItem>
</Tabs>

## Delete a branch

<Tabs groupId="async-sync">
  <TabItem value="Async" default>

  ```python
  from infrahub_sdk import InfrahubClient
  client = InfrahubClient()
  await client.branch.delete(branch_name="new-branch")
  ```

  </TabItem>
  <TabItem value="Sync" default>

  ```python
  from infrahub_sdk import InfrahubClientSync
  client = InfrahubClientSync()
  client.branch.delete(branch_name="new-branch")
  ```

  </TabItem>
</Tabs>

## Generating a diff for a branch

<Tabs groupId="async-sync">
  <TabItem value="Async" default>

  ```python
  from infrahub_sdk import InfrahubClient
  client = await InfrahubClient()
  diff = await client.branch.diff_data(branch_name="new-branch")
  ```

  </TabItem>
  <TabItem value="Sync" default>

  ```python
  from infrahub_sdk import InfrahubClientSync
  client = InfrahubClientSync()
  diff = client.branch.diff_data(branch_name="new-branch")
  ```

  </TabItem>
</Tabs>
