# Merge a branch

Merging copies a [branch](/docs/concepts/project-lifecycle.md#branches)'s changes into `main`. Merge once you have looked at the branch's [preview](/docs/projects/preview.md).

## Before you start

You need a branch with a change in it. See [Create a branch](/docs/projects/create-branch.md).

## Merge into main

**Code**

1. See what would change:

```bash
npx veryfront merge new-headline --dry-run
```

2. Merge it. `--yes` answers its "Merge branch "new-headline" into main?" question:

```bash
npx veryfront merge new-headline --yes
```

3. Read the output. You should now see `Merged "new-headline" into main`.

### Try it in a sandbox

```playground
step: Create a project | npm create -y veryfront@latest support-desk -- --template minimal --yes
step: Push it | cd support-desk && npx veryfront push
step: Change the heading | sed -i 's/Welcome to Veryfront/Support desk/' app/page.tsx
step: Push it to a branch | npx veryfront push --branch new-headline
step: See what would change | npx veryfront merge new-headline --dry-run
check: npx veryfront merge new-headline --yes
expect: Merged "new-headline" into main
```

**Studio**

1. Click the branch menu, which reads `new-headline`.
2. Click **Merge to main**. Studio lists the changed files for you to check.
3. Click **Merge to main**. You should now see "Branch merged successfully", the branch menu back on `main` and "Support desk" in `page.tsx`.

**API**

1. Set `PROJECT` to your project's ID ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)): `export PROJECT=<id>`.
2. List the branches with your [API key](/docs/authentication/api-keys.md):

```bash
curl https://api.veryfront.com/projects/$PROJECT/branches \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

3. Set `BRANCH_ID` to `new-headline`'s `id`: `export BRANCH_ID=<id>`.
4. Check that the branch can merge. `can_merge` must be `true`:

```bash
curl https://api.veryfront.com/projects/$PROJECT/branches/$BRANCH_ID/merge-eligibility \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

5. Merge it:

```bash
curl -X POST https://api.veryfront.com/projects/$PROJECT/branches/$BRANCH_ID/merge \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"merge_message":"New headline"}'
```

6. Read the response. You should now see `"success": true` and the branch's `status` as `merged`.

### Try it in a sandbox

```playground
step: Create a project | npm create -y veryfront@latest support-desk -- --template minimal --yes
step: Push it | cd support-desk && npx veryfront push
step: Change the heading | sed -i 's/Welcome to Veryfront/Support desk/' app/page.tsx
step: Push it to a branch | npx veryfront push --branch new-headline
step: Set PROJECT | export PROJECT=$VERYFRONT_PROJECT_ID
step: List the branches | curl https://api.veryfront.com/projects/$PROJECT/branches -H "Authorization: Bearer $VERYFRONT_API_KEY"
step: Set BRANCH_ID | export BRANCH_ID=$(curl -s https://api.veryfront.com/projects/$PROJECT/branches -H "Authorization: Bearer $VERYFRONT_API_KEY" | jq -r '.data[] | select(.name == "new-headline") | .id')
step: Check that it can merge | curl https://api.veryfront.com/projects/$PROJECT/branches/$BRANCH_ID/merge-eligibility -H "Authorization: Bearer $VERYFRONT_API_KEY"
check: curl -X POST https://api.veryfront.com/projects/$PROJECT/branches/$BRANCH_ID/merge -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{"merge_message":"New headline"}'
expect: "status":"merged"
```

## With MCP

1. Call `veryfront__list_branches` with `project_reference` (the project's slug or ID). Keep `new-headline`'s `id` as the `branch_id`.
2. Call `veryfront__check_branch_merge_eligibility` with the `branch_id`. `can_merge` must be `true`.
3. Call `veryfront__get_branch_merge_preview` with the `branch_id` to see the changed files.
4. Call `veryfront__merge_branch` with the `branch_id` and `"merge_message": "New headline"`. Leave out `target_branch_id` to merge into `main`.
5. Call `veryfront__get_branch` with the `branch_id`. It worked when its `status` is `merged`.

## Next steps

* [Publish](/docs/deployment/publish.md)
* [Run an eval](/docs/agents/run-eval.md)
