> ## 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.

# Git-Integration

Novita Sandbox stellt `sandbox.git` Hilfsfunktionen für gängige Repository-Workflows bereit, wie Klonen, Branching, Committen, Pulling, Pushing, Verwalten von Remotes und Konfigurieren von Git.

## Authentifizierung und Identität

### Inline-Anmeldedaten

Für private HTTPS-Repositorys übergib sowohl einen Benutzernamen als auch ein Passwort/token direkt an Vorgänge wie `push`, `pull` oder `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>

### Einmal mit dem Git-Credential-Helper authentifizieren

Du kannst `dangerouslyAuthenticate()` in JavaScript oder `dangerously_authenticate()` in Python verwenden, um Anmeldedaten im Credential-Helper der Sandbox zu speichern.

<Warning>
  Anmeldedaten werden innerhalb der Sandbox auf die Festplatte geschrieben und können von allem gelesen werden, das Zugriff auf die Sandbox hat.
</Warning>

Anmeldedaten können standardmäßig für GitHub oder für einen benutzerdefinierten HTTPS-Host gespeichert werden.

<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>

### Anmeldedaten in der Remote-URL behalten

Standardmäßig werden Anmeldedaten nach dem Klonen aus der Remote-URL entfernt. Um sie in `.git/config` beizubehalten, setze `dangerouslyStoreCredentials: true` (JS) oder `dangerously_store_credentials=True` (Python).

<Warning>
  Anmeldedaten, die in einer Remote-URL behalten werden, verbleiben in der Repository-Konfiguration und sind für Sandbox-Prozesse lesbar.
</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>

## Commit-Identität konfigurieren

Du kannst `configureUser` (JS) oder `configure_user` (Python) verwenden, um Details zum Commit-Autor global oder lokal für ein Repository festzulegen.

<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>

## Repositorys klonen

Unterstützte Klonoptionen umfassen Zielpfad, Branch-Auswahl, Tiefe, Benutzername, Passwort und Verhalten zur Speicherung von Anmeldedaten.

<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>

## Repository-Status und Branches prüfen

Du kannst `status()` verwenden, um den aktuellen Branch, ahead/behind-Zählungen und den Dateistatus zu prüfen.

Du kannst `branches()` verwenden, um die Branch-Liste und den aktuellen Branch abzurufen.

<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>

## Branches erstellen, wechseln und löschen

<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>

## Änderungen stagen und committen

Du kannst `add` verwenden, um alle Änderungen oder ausgewählte Dateien zu stagen.

Du kannst `commit` verwenden, um Commits zu erstellen. Zu den Optionen gehören benutzerdefinierter Autorenname, Autoren-E-Mail und leere Commits.

<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 und Push

`push` und `pull` können standardmäßig den konfigurierten Upstream verwenden. Du kannst auch Remote, Branch und Upstream-Konfiguration angeben.

<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>

## Remotes verwalten

Du kannst `remoteAdd` (JS) oder `remote_add` (Python) verwenden, um Remotes hinzuzufügen, optional nach dem Hinzufügen zu fetchen oder ein vorhandenes Remote zu überschreiben.

<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>

## Git-Konfiguration

Du kannst `setConfig` / `set_config` und `getConfig` / `get_config` verwenden, um Git-Einstellungen global oder pro Repository zu verwalten.

<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>
