Skip to content

Git Operations

View as Markdown

Daytona provides built-in Git support through the git module in sandboxes.

Daytona provides methods to clone, check status, and manage Git repositories in sandboxes.

Git operations assume you are operating in the sandbox user’s home directory (e.g. workspace implies /home/[username]/workspace). Use a leading / when providing absolute paths.

Clone a Git repository into a sandbox by providing the URL and path to clone it to. You can clone public or private repositories, specific branches or commits, and authenticate using personal access tokens.

# Basic clone
sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo"
)
# Clone with authentication
sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo",
username="git",
password="personal_access_token"
)
# Clone specific branch
sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo",
branch="develop"
)
# Clone a specific commit (detached HEAD)
sandbox.git.clone(
url="https://github.com/user/repo.git",
path="workspace/repo-old",
commit_id="abc123def456"
)
# Clone from a self-signed internal Git server (insecure)
sandbox.git.clone(
url="https://internal-git.example.com/org/repo.git",
path="workspace/repo",
insecure_skip_tls=True
)

Get the status of a Git repository by providing the path to the repository.

You can get the current branch, modified files, and the number of commits ahead and behind the upstream tracking branch. When no upstream is configured, ahead and behind are zero and branch_published is false. The response also includes upstream (for example origin/main) and detached when HEAD is not on a branch.

# Get repository status
status = sandbox.git.status("workspace/repo")
print(f"Current branch: {status.current_branch}")
print(f"Upstream: {status.upstream}")
print(f"Detached HEAD: {status.detached}")
print(f"Commits ahead: {status.ahead}")
print(f"Commits behind: {status.behind}")
for file in status.file_status:
print(f"File: {file.name}")
# List branches
response = sandbox.git.branches("workspace/repo")
print(f"Checked out branch: {response.current}")
for branch in response.branches:
print(f"Branch: {branch}")

Daytona provides methods to manage branches in Git repositories. You can create, switch, and delete branches. Checkout accepts a branch name or a commit SHA.

Create a new branch by providing the path to the repository and the name of the new branch.

# Create a new branch
sandbox.git.create_branch("workspace/repo", "new-feature")

Checkout a branch or commit by providing the path to the repository and the name of the branch or commit SHA. Pass a commit SHA to enter detached HEAD state.

# Checkout a branch
sandbox.git.checkout_branch("workspace/repo", "feature-branch")
# Checkout a commit (detached HEAD)
sandbox.git.checkout_branch("workspace/repo", "abc123def456")

Delete a branch by providing the path to the repository and the name of the branch.

# Delete a branch
sandbox.git.delete_branch("workspace/repo", "old-feature")

Stage specific files, all changes, or the whole repository by providing the path to the repository and the files to stage.

# Stage a single file
sandbox.git.add("workspace/repo", ["file.txt"])
# Stage multiple files
sandbox.git.add("workspace/repo", [
"src/main.py",
"tests/test_main.py",
"README.md"
])

Commit changes by providing the path to the repository, the message, author, and email.

# Stage and commit changes
sandbox.git.add("workspace/repo", ["README.md"])
sandbox.git.commit(
path="workspace/repo",
message="Update documentation",
author="John Doe",
email="john@example.com",
allow_empty=True
)

Daytona provides methods to work with remote repositories in Git. You can push and pull changes from remote repositories.

Push changes to a remote repository by providing the path to the repository and the username and password to authenticate.

# Push without authentication (for public repos or SSH)
sandbox.git.push("workspace/repo")
# Push with authentication
sandbox.git.push(
path="workspace/repo",
username="user",
password="github_token"
)

Pull changes from a remote repository by providing the path to the repository and the username and password to authenticate.

# Pull without authentication
sandbox.git.pull("workspace/repo")
# Pull with authentication
sandbox.git.pull(
path="workspace/repo",
username="user",
password="github_token"
)

Daytona provides additional Git operations through the Toolbox API.

Initialize a new Git repository by providing the path to the repository and the name of the first branch. Set bare to create a repository without a working tree.

Terminal window
curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/git/init' \
--request POST \
--header 'Content-Type: application/json' \
--data '{
"bare": false,
"initial_branch": "main",
"path": "workspace/repo"
}'

Reset the current HEAD to the specified state by providing the path to the repository, the mode and the target revision to reset to. Pass files to constrain the reset to specific paths.

Terminal window
curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/git/reset' \
--request POST \
--header 'Content-Type: application/json' \
--data '{
"files": [],
"mode": "mixed",
"path": "workspace/repo",
"target": "HEAD~1"
}'

Restore working tree files or unstage changes by providing the path to the repository, the files to restore, the source revision, and whether to restore from the staged index or working tree.

Terminal window
curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/git/restore' \
--request POST \
--header 'Content-Type: application/json' \
--data '{
"files": ["src/main.py"],
"path": "workspace/repo",
"source": "",
"staged": false,
"worktree": true
}'

Return the commit log for a repository.

Terminal window
curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/git/history?path=workspace/repo'

List configured remotes or add (and optionally overwrite) a remote by providing the path to the repository, the name of the remote, the URL of the remote, and whether to fetch from the remote immediately after adding it.

Terminal window
# List remotes
curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/git/remotes?path=workspace/repo'
# Add a remote
curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/git/remotes' \
--request POST \
--header 'Content-Type: application/json' \
--data '{
"fetch": false,
"name": "origin",
"overwrite": false,
"path": "workspace/repo",
"url": "https://github.com/user/repo.git"
}'

Read or write Git config values, or set the user name and email at a given scope by providing the path to the repository, the key to get or set, and the value to set.

Scope: global (default), local, or system.

Terminal window
# Get a config value
curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/git/config?key=user.name&scope=global'
# Set a config value
curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/git/config' \
--request POST \
--header 'Content-Type: application/json' \
--data '{
"key": "core.editor",
"path": "",
"scope": "global",
"value": "vim"
}'
# Configure user name and email
curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/git/config/user' \
--request POST \
--header 'Content-Type: application/json' \
--data '{
"email": "john@example.com",
"name": "John Doe",
"path": "",
"scope": "global"
}'

Persist Git credentials globally via the credential store by providing the host, protocol, username, and password. Credentials are stored in plaintext on disk.

Terminal window
curl 'https://proxy.app.daytona.io/toolbox/{sandboxId}/git/credentials' \
--request POST \
--header 'Content-Type: application/json' \
--data '{
"host": "github.com",
"password": "personal_access_token",
"protocol": "https",
"username": "git"
}'