# Git

Every sandbox contains a Git repository. Use the SDK to review changes, create checkpoints, work with branches, synchronize a provider repository, or clone the sandbox elsewhere.

## Review changes

```ts
const review = await sandbox.git.review();

console.log(review.files);
console.log(review.stagedPatch);
console.log(review.unstagedPatch);
```

`review()` returns the changed files and their staged and unstaged patches, making it suitable for a review interface or automated validation.

## Create a checkpoint

```ts
await sandbox.git.stage({
  paths: ['app/Http/Controllers/InventoryController.php'],
});

const checkpoint = await sandbox.git.checkpoint(
  'PHPSandbox <automation@example.com>',
  'Add inventory endpoint',
  'main',
);
```

Use `unstage()` to remove paths from the index. `revert()` discards selected changes, so use it only after reviewing the affected paths.

## Work with branches

```ts
await sandbox.git.checkout('feature/inventory', true);

const status = await sandbox.git.status();
const history = await sandbox.git.log(status.branch);
```

The second argument to `checkout()` creates the branch when it does not exist.

## Synchronize with GitHub

First link a GitHub integration and attach it to the sandbox. Then create a Git target:

```ts
const target = await sandbox.git.targets.create({
  provider: 'github',
  repository: 'acme/inventory',
  branch: 'main',
  author: {
    name: 'PHPSandbox',
    email: 'automation@example.com',
  },
  setDefault: true,
});

await target.sync({ direction: 'both' });
```

Use `pull`, `push`, or `both` for the synchronization direction. The target retains the repository and branch configuration; the attached integration supplies authorization when synchronization runs.

## Clone a sandbox repository

```ts
const credentials = await sandbox.git.credentials();
```

Use the returned URL, username, and password with a standard Git client. The credentials are short-lived: do not place them in source control, command history, logs, or a permanent remote URL.

Cloning creates another working copy of the repository. Use [Managing sandboxes](/docs/sdk/lifecycle) when you need another sandbox with its own environment.
