Skip to main content

Branch management

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

Get a single branch

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

Get all the branches

from asyncio import run as aiorun

from rich import print as rprint

from infrahub_sdk import InfrahubClient


async def main():
client = InfrahubClient(address="http://localhost:8000")
branches = await client.branch.all()
rprint(branches)


if __name__ == "__main__":
aiorun(main())

Create a branch

from asyncio import run as aiorun

from infrahub_sdk import InfrahubClient


async def main():
client = InfrahubClient(address="http://localhost:8000")
await client.branch.create(branch_name="new-branch", description="description", sync_with_git=False)
print("New branch created")


if __name__ == "__main__":
aiorun(main())

Rebase a branch

from asyncio import run as aiorun

from infrahub_sdk import InfrahubClient


async def main():
client = InfrahubClient(address="http://localhost:8000")
await client.branch.rebase(branch_name="new-branch")


if __name__ == "__main__":
aiorun(main())

Merge a branch

from asyncio import run as aiorun

from infrahub_sdk import InfrahubClient


async def main():
client = InfrahubClient(address="http://localhost:8000")
await client.branch.merge(branch_name="new-branch")


if __name__ == "__main__":
aiorun(main())

Delete a branch

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

Getting the diff for a branch

Use get_diff_tree to retrieve the full diff of a branch compared to its base branch, including summary counts and the list of changed nodes. It returns None if no diff exists for the branch. Set include_properties=True to also retrieve the value-level details of each change (previous and new value per property).

from infrahub_sdk import InfrahubClient
client = InfrahubClient()
diff = await client.get_diff_tree(branch="new-branch", include_properties=True)

If you only need the list of changed nodes, get_diff_summary returns them without the diff metadata.

from infrahub_sdk import InfrahubClient
client = InfrahubClient()
node_diffs = await client.get_diff_summary(branch="new-branch")