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

# Integração com Git

O Novita Sandbox expõe auxiliares `sandbox.git` para fluxos de trabalho comuns de repositórios, como clonar, criar branches, fazer commits, fazer pull, fazer push, gerenciar remotes e configurar o Git.

## Autenticação e identidade

### Credenciais inline

Para repositórios HTTPS privados, passe um nome de usuário e uma senha/token diretamente em operações como `push`, `pull` ou `clone`.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  import { Sandbox } from 'novita-sandbox'

  const sandbox = await Sandbox.create()

  await sandbox.git.push(repoPath, {
    username: process.env.GIT_USERNAME,
    password: process.env.GIT_TOKEN,
  })

  await sandbox.git.pull(repoPath, {
    username: process.env.GIT_USERNAME,
    password: process.env.GIT_TOKEN,
  })
  ```

  ```python Python icon="python" theme={"system"}
  from novita_sandbox.core import Sandbox
  import os

  sandbox = Sandbox.create()

  sandbox.git.push(
      repo_path,
      username=os.environ.get("GIT_USERNAME"),
      password=os.environ.get("GIT_TOKEN"),
  )

  sandbox.git.pull(
      repo_path,
      username=os.environ.get("GIT_USERNAME"),
      password=os.environ.get("GIT_TOKEN"),
  )
  ```
</CodeGroup>

### Autentique uma vez com o auxiliar de credenciais do Git

Você pode usar `dangerouslyAuthenticate()` em JavaScript ou `dangerously_authenticate()` em Python para armazenar credenciais dentro do auxiliar de credenciais do sandbox.

<Warning>
  As credenciais são gravadas em disco dentro do sandbox e podem ser lidas por qualquer coisa com acesso ao sandbox.
</Warning>

As credenciais podem ser armazenadas para o GitHub por padrão ou para um host HTTPS personalizado.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  await sandbox.git.dangerouslyAuthenticate({
    username: process.env.GIT_USERNAME,
    password: process.env.GIT_TOKEN,
  })

  await sandbox.git.dangerouslyAuthenticate({
    username: process.env.GIT_USERNAME,
    password: process.env.GIT_TOKEN,
    host: 'git.example.com',
    protocol: 'https',
  })

  await sandbox.git.clone('https://git.example.com/org/repo.git', {
    path: '/home/user/repo',
  })

  await sandbox.git.push('/home/user/repo')
  ```

  ```python Python icon="python" theme={"system"}
  sandbox.git.dangerously_authenticate(
      username=os.environ.get("GIT_USERNAME"),
      password=os.environ.get("GIT_TOKEN"),
  )

  sandbox.git.dangerously_authenticate(
      username=os.environ.get("GIT_USERNAME"),
      password=os.environ.get("GIT_TOKEN"),
      host="git.example.com",
      protocol="https",
  )

  sandbox.git.clone(
      "https://git.example.com/org/repo.git",
      path="/home/user/repo",
  )

  sandbox.git.push("/home/user/repo")
  ```
</CodeGroup>

### Mantenha as credenciais na URL remota

Por padrão, as credenciais são removidas da URL remota após o clone. Para mantê-las em `.git/config`, defina `dangerouslyStoreCredentials: true` (JS) ou `dangerously_store_credentials=True` (Python).

<Warning>
  Credenciais mantidas em uma URL remota permanecem na configuração do repositório e podem ser lidas por processos do sandbox.
</Warning>

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  await sandbox.git.clone('https://git.example.com/org/repo.git', {
    path: '/home/user/repo',
    username: process.env.GIT_USERNAME,
    password: process.env.GIT_TOKEN,
  })

  await sandbox.git.clone('https://git.example.com/org/repo.git', {
    path: '/home/user/repo',
    username: process.env.GIT_USERNAME,
    password: process.env.GIT_TOKEN,
    dangerouslyStoreCredentials: true,
  })
  ```

  ```python Python icon="python" theme={"system"}
  sandbox.git.clone(
      "https://git.example.com/org/repo.git",
      path="/home/user/repo",
      username=os.environ.get("GIT_USERNAME"),
      password=os.environ.get("GIT_TOKEN"),
  )

  sandbox.git.clone(
      "https://git.example.com/org/repo.git",
      path="/home/user/repo",
      username=os.environ.get("GIT_USERNAME"),
      password=os.environ.get("GIT_TOKEN"),
      dangerously_store_credentials=True,
  )
  ```
</CodeGroup>

## Configure a identidade de commit

Você pode usar `configureUser` (JS) ou `configure_user` (Python) para definir os detalhes do autor do commit globalmente ou localmente para um repositório.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  await sandbox.git.configureUser('Novita Bot', 'bot@example.com')

  await sandbox.git.configureUser('Novita Bot', 'bot@example.com', {
    scope: 'local',
    path: repoPath,
  })
  ```

  ```python Python icon="python" theme={"system"}
  sandbox.git.configure_user("Novita Bot", "bot@example.com")

  sandbox.git.configure_user(
      "Novita Bot",
      "bot@example.com",
      scope="local",
      path=repo_path,
  )
  ```
</CodeGroup>

## Clone repositórios

As opções de clone compatíveis incluem caminho de destino, seleção de branch, profundidade, nome de usuário, senha e comportamento de armazenamento de credenciais.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  await sandbox.git.clone(repoUrl, {
    path: repoPath,
  })

  await sandbox.git.clone(repoUrl, {
    path: repoPath,
    branch: 'main',
  })

  await sandbox.git.clone(repoUrl, {
    path: repoPath,
    depth: 1,
  })
  ```

  ```python Python icon="python" theme={"system"}
  sandbox.git.clone(repo_url, path=repo_path)

  sandbox.git.clone(repo_url, path=repo_path, branch="main")

  sandbox.git.clone(repo_url, path=repo_path, depth=1)
  ```
</CodeGroup>

## Verifique o status do repositório e as branches

Você pode usar `status()` para inspecionar a branch atual, contagens de ahead/behind e status dos arquivos.

Você pode usar `branches()` para obter a lista de branches e a branch atual.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  const status = await sandbox.git.status(repoPath)
  console.log(status.currentBranch)
  console.log(status.ahead)
  console.log(status.behind)
  console.log(status.fileStatus)

  const branches = await sandbox.git.branches(repoPath)
  console.log(branches.currentBranch)
  console.log(branches.branches)
  ```

  ```python Python icon="python" theme={"system"}
  status = sandbox.git.status(repo_path)
  print(status.current_branch)
  print(status.ahead)
  print(status.behind)
  print(status.file_status)

  branches = sandbox.git.branches(repo_path)
  print(branches.current_branch)
  print(branches.branches)
  ```
</CodeGroup>

## Crie, alterne e exclua branches

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  await sandbox.git.createBranch(repoPath, 'feature/new-docs')

  await sandbox.git.checkoutBranch(repoPath, 'main')

  await sandbox.git.deleteBranch(repoPath, 'feature/old-docs')

  await sandbox.git.deleteBranch(repoPath, 'feature/stale-docs', {
    force: true,
  })
  ```

  ```python Python icon="python" theme={"system"}
  sandbox.git.create_branch(repo_path, "feature/new-docs")

  sandbox.git.checkout_branch(repo_path, "main")

  sandbox.git.delete_branch(repo_path, "feature/old-docs")

  sandbox.git.delete_branch(
      repo_path,
      "feature/stale-docs",
      force=True,
  )
  ```
</CodeGroup>

## Faça stage e commit das alterações

Você pode usar `add` para fazer stage de todas as alterações ou de arquivos selecionados.

Você pode usar `commit` para criar commits. As opções incluem nome de autor personalizado, e-mail do autor e commits vazios.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  await sandbox.git.add(repoPath)

  await sandbox.git.commit(repoPath, 'Initial commit')

  await sandbox.git.add(repoPath, {
    files: ['README.md', 'src/index.ts'],
  })

  await sandbox.git.commit(repoPath, 'Docs sync', {
    authorName: 'Novita Bot',
    authorEmail: 'bot@example.com',
    allowEmpty: true,
  })
  ```

  ```python Python icon="python" theme={"system"}
  sandbox.git.add(repo_path)

  sandbox.git.commit(repo_path, "Initial commit")

  sandbox.git.add(
      repo_path,
      files=["README.md", "src/index.ts"],
  )

  sandbox.git.commit(
      repo_path,
      "Docs sync",
      author_name="Novita Bot",
      author_email="bot@example.com",
      allow_empty=True,
  )
  ```
</CodeGroup>

## Pull e push

`push` e `pull` podem usar o upstream configurado por padrão. Você também pode especificar remote, branch e configuração de upstream.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  await sandbox.git.push(repoPath)

  await sandbox.git.pull(repoPath)

  await sandbox.git.push(repoPath, {
    remote: 'origin',
    branch: 'main',
    setUpstream: true,
  })

  await sandbox.git.pull(repoPath, {
    remote: 'origin',
    branch: 'main',
  })
  ```

  ```python Python icon="python" theme={"system"}
  sandbox.git.push(repo_path)

  sandbox.git.pull(repo_path)

  sandbox.git.push(
      repo_path,
      remote="origin",
      branch="main",
      set_upstream=True,
  )

  sandbox.git.pull(
      repo_path,
      remote="origin",
      branch="main",
  )
  ```
</CodeGroup>

## Gerencie remotes

Você pode usar `remoteAdd` (JS) ou `remote_add` (Python) para adicionar remotes, opcionalmente fazer fetch após adicionar, ou sobrescrever um remote existente.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  await sandbox.git.remoteAdd(repoPath, 'origin', repoUrl)

  await sandbox.git.remoteAdd(repoPath, 'origin', repoUrl, {
    fetch: true,
  })

  await sandbox.git.remoteAdd(repoPath, 'origin', repoUrl, {
    overwrite: true,
  })
  ```

  ```python Python icon="python" theme={"system"}
  sandbox.git.remote_add(repo_path, "origin", repo_url)

  sandbox.git.remote_add(
      repo_path,
      "origin",
      repo_url,
      fetch=True,
  )

  sandbox.git.remote_add(
      repo_path,
      "origin",
      repo_url,
      overwrite=True,
  )
  ```
</CodeGroup>

## Configuração do Git

Você pode usar `setConfig` / `set_config` e `getConfig` / `get_config` para gerenciar configurações do Git globalmente ou por repositório.

<CodeGroup>
  ```js JavaScript & TypeScript icon="js" theme={"system"}
  await sandbox.git.setConfig('pull.rebase', 'false')

  const value = await sandbox.git.getConfig('pull.rebase')

  await sandbox.git.setConfig('pull.rebase', 'false', {
    scope: 'local',
    path: repoPath,
  })

  const localValue = await sandbox.git.getConfig('pull.rebase', {
    scope: 'local',
    path: repoPath,
  })
  ```

  ```python Python icon="python" theme={"system"}
  sandbox.git.set_config("pull.rebase", "false")

  value = sandbox.git.get_config("pull.rebase")

  sandbox.git.set_config(
      "pull.rebase",
      "false",
      scope="local",
      path=repo_path,
  )

  local_value = sandbox.git.get_config(
      "pull.rebase",
      scope="local",
      path=repo_path,
  )
  ```
</CodeGroup>
