> ## Documentation Index
> Fetch the complete documentation index at: https://empirical.run/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Branching

> Run feature-branch test changes against preview deployments, and merge them when the application branch merges

By default, every test run uses the test code on the test repository's default branch. When a
feature changes the UI, its tests have to change too, but only for runs of that feature: the same
change would fail against a deployment that doesn't have the feature yet.

Branching keeps test changes on a test branch with the same name as the application branch. Runs for
that feature use the test branch, and Empirical merges it into the default branch when the
application branch merges.

![Without branching, a preview deployment of a feature runs the default branch's tests and fails](https://mintcdn.com/empirical/nk5asG_4jln8aEt4/images/branching/preview-without-branching.png?fit=max\&auto=format\&n=nk5asG_4jln8aEt4\&q=85\&s=db11beff6d266568947ecbf3053de843)

![With branching, the preview deployment runs the matching test branch](https://mintcdn.com/empirical/nk5asG_4jln8aEt4/images/branching/preview-with-branching.png?fit=max\&auto=format\&n=nk5asG_4jln8aEt4\&q=85\&s=0008f2d99b503c697dcbdb20d13cb056)

## Which test code a run uses

| Trigger | Test code |
| - | - |
| `pull_request` trigger | The test branch with the same name as the pull request's head branch, merged into the default branch. If there is no such test branch, the default branch. See [Pull request runs](#pull-request-runs). |
| `branch` on the trigger request | That test branch as it is, without a merge. If the branch doesn't exist, the run errors. `branch` takes precedence over a `pull_request` trigger. |
| Neither | The default branch. |

Test runs never create a test branch. It must already exist in the test repository.

## Pull request runs

A run triggered for an application pull request tests the test code as it will look once that pull
request merges. Empirical merges the matching test branch into the default branch on a temporary
`test-run/merge/*` ref and runs the merge commit. It deletes these refs after the test branch merges.

If the test branch conflicts with the default branch, the run errors with a merge conflict. Rebase
the test branch, or pass `branch` to run it without a merge.

The [dispatch action](https://empirical.run/docs/triggers/ci-cd.md#github-actions) sends the trigger on `pull_request` workflow
events. With the API, send `trigger` together with `build`:

```json theme={null}
{
  "environment": "preview",
  "build": { "repo": "acme/web", "branch": "feat-x", "commit": "<sha>" },
  "trigger": {
    "event": "pull_request",
    "pull_request": { "number": 123, "head": "feat-x", "base": "main" }
  }
}
```

## Choose the default branch

New projects use `main`. To change it, open **Settings → General**.

Pick the branch your application merges features into. For example, if features merge to a
`staging` branch in your application repository, name the Empirical default branch `staging` too.

## Merge test branches with application branches

When an application pull request merges, Empirical needs to merge the matching test branch into the
same base. Add a merge step to your CI so Empirical hears about the merge. It sends only the branch
names, not your application code. Empirical deletes the test branch after it merges.

Empirical merges only an open, ready-for-review pull request from the test branch into that base.
If the test branch has no open pull request, or its pull request is a draft, nothing merges. To keep
a test change out of the default branch, close its pull request or mark it as a draft.

### GitHub Actions

Add this workflow to `.github/workflows/merge-branches.yaml`. The action reads the head and base
branch from the `pull_request` event:

```yaml theme={null}
name: Merge branches in Empirical

on:
  pull_request:
    types: [closed]

jobs:
  auto-merge:
    if: github.event.pull_request.merged == true
    runs-on: ubuntu-latest
    steps:
      - name: Merge branches in Empirical
        uses: empirical-run/merge-branches-action@v1
        with:
          auth-key: ${{ secrets.EMPIRICALRUN_KEY }}
```

`v1` receives backwards-compatible fixes, and breaking changes ship as a new major version. To pin
an exact release, use a tag such as `v1.0.0`.

### Other CI/CD providers

Call the merge-branches API after the application pull request merges. Set `HEAD_BRANCH` to the
branch that merged and `BASE_BRANCH` to the branch it merged into:

```sh theme={null}
curl -X POST https://api.empirical.run/api/projects/merge-branches \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $EMPIRICALRUN_KEY" \
  -d "{\"head_branch\": \"$HEAD_BRANCH\", \"base_branch\": \"$BASE_BRANCH\"}"
```

## Related

* [Linked test changes](https://empirical.run/docs/features/linked-test-changes.md): block an application pull request until its test branch merges
* [Trigger runs from CI/CD](https://empirical.run/docs/triggers/ci-cd.md): send `pull_request` triggers and build metadata


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.