1---
2title: Branches
3description: Prisma branches group databases under git-style names — preview branches per stage, default-branch promotion, and database attachment.
4---
5
6A `Prisma.Branch` groups databases and apps under a git-style name
7inside a project. Prisma tracks two separate properties:
8
9- `role` controls whether Compute resolves `production` or `preview`
10 environment variables.
11- `isDefault` identifies the branch new resources use when they omit
12 a branch.
13
14The first branch created with a project has the immutable
15`production` role. Additional branches have the `preview` role.
16Promoting a preview branch changes `isDefault`; it does not change
17that branch's role.
18
19```typescript
20import * as Prisma from "alchemy/Prisma";
21
22const branch = yield* Prisma.Branch("preview", { project });
23```
24
25`gitName` is optional — omitted, alchemy generates a stable physical
26name. Pass one when the branch should mirror a real git branch:
27
28```typescript
29const branch = yield* Prisma.Branch("feature", {
30 project,
31 gitName: "feature/search",
32});
33```
34
35## Attaching databases
36
37A database joins a branch by `branchId` (or `branchGitName`):
38
39```typescript
40const previewDb = yield* Prisma.Postgres("preview-db", {
41 project,
42 branchId: branch.branchId,
43});
44```
45
46Compute apps attach the same way. Environment-variable scope follows
47the branch's `role`, not `isDefault`, so a promoted preview branch
48continues to use the `preview` class.
49
50## A branch per stage
51
52Branch names can come from the Stack's stage, giving every preview
53stage its own branch — and the databases and apps attached to it —
54that tears down with the stage:
55
56```typescript
57import { Stage } from "alchemy";
58
59const stage = yield* Stage;
60
61const branch = yield* Prisma.Branch("stage", {
62 project,
63 gitName: `preview/${stage}`,
64});
65```
66
67## Default-branch promotion
68
69`isDefault: true` promotes the branch, atomically clearing the current
70default flag. The Management API only supports promotion — demotion
71happens by promoting another branch — and alchemy records the
72displaced branch so destroying the resource can restore it:
73
74```typescript
75const release = yield* Prisma.Branch("release", {
76 project,
77 gitName: "release/next",
78 isDefault: true,
79});
80```
81
82The original `production`-role branch is owned by the project and
83cannot be deleted through `Prisma.Branch`. A preview-role
84`Prisma.Branch` promoted to default remains owned by its Branch
85resource: on destroy, alchemy restores the displaced default and then
86deletes the promoted branch.
87
88## Where next
89
90- [Postgres](/prisma/data/postgres) — projects and databases.
91- [Connections](/prisma/data/connections) — credentials for the
92 databases on a branch.
93
94Reference:
95
96- [Branch API reference](/providers/prisma/branch)