# Preview and apply

> Preview resolves a package or a resource at the head of the branch and shows what would change, without writing. Apply writes exactly the commit you previewed, skips what did not change and can publish new versions.

Source: https://docs.sommatic.ai/en/git-sync/preview-and-apply/ (updated 2026-11-26)

Deploying from Git is two calls. **Preview** tells you which commit the installation's branch is at and what each
resource would become. **Apply** writes that commit, and only that commit. The same operations are behind
**Sync package** and **Sync from Git…** in the portal.

The examples use two placeholders: `$SOMMATIC_API` is the API base URL (`https://api.sommatic.ai`) and
`$SOMMATIC_TOKEN` is a Bearer token for your organization (see [Authentication](https://docs.sommatic.ai/en/api/authentication.md)).
`<repository_id>` is the id of the Git repository connection, for example `pi-git-repo-xxxxxxxxxxxxxxx`. Every
response uses the standard envelope `{ status, success, message, result }`.

## Choose what to sync

Preview and apply select the same way:

| Parameter     | Selects                                                           |
| ------------- | ----------------------------------------------------------------- |
| `path`        | Every resource of the package in that folder.                     |
| `key`         | One resource, by its logical key.                                 |
| `resource_id` | One resource, by its id in this installation.                     |
| `keys`        | Apply only: together with `path`, only these keys of the package. |

### Read the package

```bash
curl -s "$SOMMATIC_API/platform-infrastructure/git-repository/<repository_id>/package?path=support" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN"
```

The result lists the manifest, the head commit of the branch and, for each resource, its `resource_id` in this
installation (`null` when the key is not mapped in the environment file).

### Preview the change

```bash
curl -s -X POST "$SOMMATIC_API/platform-infrastructure/git-sync/preview" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{ "repository_id": "<repository_id>", "path": "support", "include_content": false }'
```

Preview writes nothing. Read these fields of `result`:

- `commit`, `commit_message`, `commit_date`: the head of the branch. This is the commit you will apply.
- `items[]`, one per resource, with `key`, `path`, `resource_id` and:
  - `exists`: whether the resource exists in this installation.
  - `unresolved`: tokens that could not be resolved. It must be empty.
  - `changes.has_changes`, and for workflows `changes.nodes` (`added`, `removed`, `changed`), `changes.edges`
    (`added`, `removed`), `changes.fields` and `changes.layout_changed`. `changes.details` names each node and the
    config settings that changed.
  - `unlinked_here`: the resource is unlinked from Git in this installation and will be skipped.

`include_content: false` leaves out the resolved content of each resource. Keep it off unless you need it.

### Apply the reviewed commit

```bash
curl -s -X POST "$SOMMATIC_API/platform-infrastructure/git-sync/apply" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
    "repository_id": "<repository_id>",
    "path": "support",
    "expected_commit": "<commit from the preview>",
    "publish": true,
    "description": "Pull request #12"
  }'
```

The result has the `commit`, whether it `published`, the counts `applied`, `unchanged`, `skipped` and `failed`, and
`items[]` with each `result`, a `reason` when it failed or was skipped, and the published `version`.

### Check the outcome

A response with `success: true` can still contain failed items. Check `result.failed` and every item's `result`
before you report the deployment as done.

## Safety rules

1. **`expected_commit` is mandatory** and must be the commit returned by the preview **and still the head of the
   branch** the connection reads. If the branch moved after your preview, apply fails: preview again and review the
   new commit. Apply never deploys an arbitrary commit, such as one of a `feature/*` branch that skipped the pull
   request, and it ignores `ref`.
2. **Nothing half-resolved is written.** A resource with unresolved tokens, or that does not exist in the
   installation, is reported as `failed` and left untouched.
3. **Apply is idempotent.** Resources without changes are `unchanged`. With `publish: true`, an unchanged resource is
   published only if its deployed version does not come from this commit yet. Running the same apply twice is safe.
4. **Rollbacks happen in Git.** To undo a change, merge a revert pull request and apply the new head. You cannot apply
   an old commit.

> **Warning: Applying writes the draft**
>
> Apply writes the workflow **draft**. Webhooks called in `test` mode run the draft, so in an installation that
> serves traffic through test mode, applying is already deploying, even without `publish`. Use `publish: true` to
> create the version that `prod` mode runs.

## Publish

With `publish: true`, apply creates a new version of each workflow and a new version snapshot of each knowledge
resource, and stores the commit, the key and your `description` with it. You can also publish later, one resource at a
time:

```bash
curl -s -X POST "$SOMMATIC_API/workflow-orchestration/control-plane/flow-design/flow-definition/<flow_definition_id>/publish" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN"

curl -s -X POST "$SOMMATIC_API/cognitive-infrastructure/cognitive-assets/knowledge-resource/<knowledge_resource_id>/publish" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN"
```

After a sync, each resource keeps a `git_binding` with the repository, branch, environment, key and file, the
`last_synced_commit` and, once published, the `deployed_commit` and `deployed_version`.

> **Note: Running instances may need a moment**
>
> The orchestrator caches workflow definitions. A newly applied or published workflow may take effect after that
> cache refreshes.

## Preview another branch

Preview accepts `ref` (a branch, tag or commit) to resolve a commit other than the head of the connection branch, for
example to check a pull request before merging it. Apply ignores `ref`: only the head of the connection branch can be
deployed.

## Next steps

- [Git Sync API reference](https://docs.sommatic.ai/en/api/reference/git-sync.md): Every Git Sync endpoint and its parameters.
- [Export and unlink](https://docs.sommatic.ai/en/git-sync/export-and-unlink.md): Bring studio changes into Git, or stop syncing a resource.
