---
title: "Git API"
description: "Read and modify repository data through Freestyle Git APIs."
url: "/docs/git/api"
---

Use the Git API when your application needs repository data without cloning locally.


## Get File Or Directory Contents

```ts
import { freestyle } from "freestyle";

const repo = freestyle.git.repos.ref({
  repoId: "your-repo-id",
});

const file = await repo.contents.get({
  path: "src/index.ts",
  rev: "main",
});

console.log(file);
```

Directory responses include nested entries:

```ts
const dir = await repo.contents.get({
  path: "src",
  rev: "main",
});

console.log(dir);
```


## Download Archives

```ts
import { writeFile } from "node:fs/promises";

const tarball = await repo.contents.downloadTarball({ rev: "main" });
await writeFile("repo.tar", Buffer.from(tarball));

const zip = await repo.contents.downloadZip({ rev: "main" });
await writeFile("repo.zip", Buffer.from(zip));
```


## Branches

```ts
const branch = await repo.branches.get({ branchName: "main" });
console.log(branch.sha);

const branches = await repo.branches.list();
console.log(branches);
```

Create a branch:

```ts
await repo.branches.create({
  name: "feature/new-flow",
});

await repo.branches.create({
  name: "feature/from-commit",
  sha: "a1b2c3d4e5f6",
});
```

Set the default branch:

```ts
await repo.branches.setDefaultBranch({
  defaultBranch: "main",
});
```


## Commits

List commits:

```ts
const commits = await repo.commits.list();
console.log(commits);
```

Create a commit by writing file changes:

```ts
await repo.commits.create({
  branch: "main",
  message: "Update README",
  files: [{ path: "README.md", content: "# Updated README\n" }],
});
```


## Tags

```ts
const tags = await repo.tags.list();

const tag = await repo.tags.get({
  tagName: "v1.0.0",
});

console.log(tag);
```
