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

# Commit without git

> Make a commit from a list of file changes with one API call, without cloning the repo.

`repo.commit()` makes one commit from a list of file changes and moves a branch to it. There's no clone, no working
tree, and no `git` binary, so it works from a serverless function, an edge runtime, or an agent tool that only needs to
write a few files. The commit is a normal git commit: anyone who fetches the branch sees it, and it sends a
[push webhook](https://docs.archil.com/git/webhooks.md) like a push does.

```typescript TypeScript theme={null}
const { commit, parent } = await repo.commit({
  branch: "main",
  message: "Update README",
  author: { name: "Agent", email: "agent@example.com" },
  files: [
    { path: "README.md", content: "hello\n" },
    { path: "bin/run.sh", content: "#!/bin/sh\necho run\n", mode: "100755" },
    { path: "logo.png", content: pngBytes }, // Uint8Array
    { path: "old.txt", delete: true },
  ],
});
// commit is the new commit's SHA; parent is null for a root commit
```

```python Python theme={null}
from archil.git import CommitFile, CommitIdentity

result = repo.commit(
    branch="main",
    message="Update README",
    author=CommitIdentity(name="Agent", email="agent@example.com"),  # or a {"name", "email"} dict
    files=[
        CommitFile(path="README.md", content="hello\n"),
        CommitFile(path="bin/run.sh", content="#!/bin/sh\necho run\n", mode="100755"),
        {"path": "logo.png", "content": png_bytes},  # dicts work too
        CommitFile(path="old.txt", delete=True),
    ],
)
result.commit, result.parent  # parent is None for a root commit
```

The same call is `archil.git.repos.commit(repoId, params)` when you have only the ID.

## Parameters

| TypeScript | Python | |
| - | - | - |
| `branch` | `branch` | A short branch name, without `refs/heads/`. It doesn't have to exist yet |
| `message` | `message` | The commit message |
| `author` | `author` | `{ name, email }` |
| `committer` | `committer` | Optional. Defaults to `author` |
| `files` | `files` | The changes, below |
| `baseRef` | `base_ref` | Optional. Where a new branch starts: a branch, tag, full ref, or commit SHA. Ignored when the branch exists |
| `expectedHead` | `expected_head` | Optional. Refuse the commit unless the branch's head is this commit. See [Check the branch head](#check-the-branch-head) |

Each entry in `files` either writes a file or deletes one:

* **Write:** `path` and `content`. A string is sent as UTF-8, and bytes (`Uint8Array` or `bytes`) as base64. `mode` is
  `100644` for a regular file (the default), `100755` for an executable, or `120000` for a symlink whose content is its
  target.
* **Delete:** `path` and `delete: true`. The path must be a file in the parent commit. Directories can't be deleted as a
  whole: delete each file in them.

Paths are relative and slash-separated, and each path can appear once per call. Deletes apply before writes, so you
can delete the file `docs` and write `docs/index.md` in one commit.

## Which parent the commit gets

1. If `branch` exists, its head. `baseRef` is ignored.
2. If `branch` doesn't exist and `baseRef` is set, the commit `baseRef` names. This creates the branch.
3. Otherwise, no parent: a root commit. That's allowed only in an empty repo, so the first commit to a new repo needs no
   `baseRef`.

To start a feature branch from `main`:

```typescript TypeScript theme={null}
await repo.commit({
  branch: "agent/fix-typo",
  baseRef: "main",
  message: "Fix typo",
  author,
  files: [{ path: "docs/intro.md", content: fixed }],
});
```

```python Python theme={null}
repo.commit(
    branch="agent/fix-typo",
    base_ref="main",
    message="Fix typo",
    author=author,
    files=[CommitFile(path="docs/intro.md", content=fixed)],
)
```

## Check the branch head

A commit and a push to the same branch never interleave: they run one after the other. Without `expectedHead`, a commit
applies its changes on top of whatever the branch's head is when it runs, which may include someone else's push.

Pass `expectedHead` to commit only if the branch hasn't moved since you read it. If it has, the call fails with code
`head_mismatch`, and `currentHead` holds the branch's head, or null if the branch doesn't exist. Pass 40 zeros to
require that the branch doesn't exist yet.

```typescript TypeScript theme={null}
import { ArchilGitError } from "disk";

const { refs } = await repo.refs();
const head = refs.find((r) => r.name === "refs/heads/main")!.target;
const current = new TextDecoder().decode(await repo.blob(head, "counter.txt"));

try {
  await repo.commit({
    branch: "main",
    message: "Increment counter",
    author,
    files: [{ path: "counter.txt", content: String(Number(current) + 1) }],
    expectedHead: head,
  });
} catch (err) {
  if (err instanceof ArchilGitError && err.code === "head_mismatch") {
    console.log("main moved to", err.currentHead, "- read it again and retry");
  } else {
    throw err;
  }
}
```

```python Python theme={null}
from archil.git import ArchilGitError

refs = repo.refs()
head = next(r.target for r in refs.refs if r.name == "refs/heads/main")
current = repo.blob(head, "counter.txt").decode()

try:
    repo.commit(
        branch="main",
        message="Increment counter",
        author=author,
        files=[CommitFile(path="counter.txt", content=str(int(current) + 1))],
        expected_head=head,
    )
except ArchilGitError as e:
    if e.code != "head_mismatch":
        raise
    print("main moved to", e.current_head, "- read it again and retry")
```

`repo.commit()` returns the new commit's SHA, so a loop that makes several commits in a row can pass the previous
result's `commit` as the next call's `expectedHead`.

## Errors

| Code | When |
| - | - |
| `head_mismatch` | The branch's head isn't `expectedHead`. `currentHead` has the actual head |
| `path_conflict` | A file would be written where a directory is, or the reverse |
| `path_not_found` | A delete names a path that isn't a file in the parent commit |
| `branch_not_found` | The branch doesn't exist, there's no `baseRef`, and the repo isn't empty |
| `base_ref_not_found` | `baseRef` names no commit |

Every error is an `ArchilGitError`. See [Errors](https://docs.archil.com/git/sdk.md#errors).

## Limits

| | |
| - | - |
| Files per commit | 10,000 |
| Request size | 64 MiB, after base64 encoding of binary content |
| Path length | 4,096 bytes |

For larger changes, clone and push with `git`. See the [quickstart](https://docs.archil.com/git/introduction.md#4-mint-a-token-and-push).


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