Skip to content

Branches and Git Operations

The Git API (Level 2) provides programmatic access to branches, commit history, and diffs without needing a local git binary.

Status: log() and diff() are fully functional. createBranch(), listBranches(), deleteBranch(), and merge() require server-side RPCs that haven’t shipped yet — calling them will throw an error.

import { Client } from "@githosted/sdk";
const client = new Client();
const repo = client.repo("my-project");
// View commit history
const commits = await repo.log({ limit: 10 });
// Get a diff between two branches
const diff = await repo.diff("main", "feature/new-api");

Get the commit log.

// Recent commits
const commits = await repo.log({ limit: 20 });
for (const c of commits) {
console.log(`${c.hash.slice(0, 7)} ${c.subject} (${c.authorName})`);
}
// Commits on a specific branch
const commits = await repo.log({ ref: "develop", limit: 10 });
// Commits touching a specific file
const history = await repo.log({ path: "src/main.ts", limit: 5 });

Each commit has: hash, authorName, authorEmail, committedAt (Date), subject.

Get the diff between two refs.

const diff = await repo.diff("main", "feature/new-api");
console.log(diff.patch); // unified diff output

Create a new branch.

const branch = await repo.createBranch("feature/new-api", { from: "main" });

List all branches.

const branches = await repo.listBranches();
for (const b of branches) {
console.log(`${b.name} ${b.isDefault ? "(default)" : ""}`);
}

Merge a branch.

const result = await repo.merge("feature/new-api", {
into: "main",
message: "Merge feature/new-api",
});