# Merge branch

```http
POST /projects/{project_reference}/branches/{branch_id}/merge
```

Merge a branch into main or another branch, applying its file changes, deletions and the merge message to the target. Requires project.files.write.

## Parameters

| Name                | In   | Required | Description                                                |
| ------------------- | ---- | -------- | ---------------------------------------------------------- |
| `project_reference` | path | Yes      | Project slug, UUID, or domain that identifies the project. |
| `branch_id`         | path | Yes      |                                                            |

## Request body

| Field              | Type         | Required | Description                                                                                                                                     |
| ------------------ | ------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `expected_version` | integer,null |          | Optional optimistic-concurrency precondition against the persisted source branch version. A stale value returns 412 before merge writes.        |
| `target_branch_id` | string,null  |          | Target branch ID; omit for main. An unknown target returns not found. Merge eligibility and conflicts are checked again when the merge commits. |
| `merge_message`    | string       |          | Message stored on the merge audit record.                                                                                                       |

## Responses

| Status | Description                                                                                                 |
| ------ | ----------------------------------------------------------------------------------------------------------- |
| 200    | Branch merged successfully.                                                                                 |
| 400    | Bad request                                                                                                 |
| 401    | Unauthorized                                                                                                |
| 403    | Forbidden                                                                                                   |
| 404    | Not found                                                                                                   |
| 409    | Conflict                                                                                                    |
| 412    | Precondition failed                                                                                         |
| 413    | Payload too large                                                                                           |
| 500    | Server error                                                                                                |
| 503    | internal: authentication state is temporarily unavailable and the request was not processed; retry shortly. |
