Git Operations | Daytona

Git Operations

Git operations are available through the git module of a sandbox. Operations run through the Daytona API, so your application works with repositories in a sandbox directly, without installing Git clients or executing shell commands inside it.

The git module covers cloning repositories, checking status, managing branches, staging and committing changes, pushing and pulling with authentication, and inspecting commit history. Private repositories authenticate with personal access tokens passed per operation.

Basic operations

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 repositories

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
)
// Basic clone

await sandbox.git.clone(

"https://github.com/user/repo.git",

"workspace/repo"
);

// Clone with authentication

await sandbox.git.clone(

"https://github.com/user/repo.git",

"workspace/repo",

undefined,

undefined,

"git",

"personal_access_token"
);

// Clone specific branch

await sandbox.git.clone(

"https://github.com/user/repo.git",

"workspace/repo",

"develop"
);

// Clone a specific commit (detached HEAD)

await sandbox.git.clone(

"https://github.com/user/repo.git",

"workspace/repo-old",

undefined,

"abc123def456"
);

// Clone from a self-signed internal Git server (insecure)

await sandbox.git.clone(

"https://internal-git.example.com/org/repo.git",

"workspace/repo",

undefined,

undefined,

undefined,

undefined,
    true
);
# 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 repository status

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}")
// Get repository status

const status = await sandbox.git.status("workspace/repo");

console.log(`Current branch: ${status.currentBranch}`);

console.log(`Upstream: ${status.upstream}`);

console.log(`Detached HEAD: ${status.detached}`);

console.log(`Commits ahead: ${status.ahead}`);

console.log(`Commits behind: ${status.behind}`);

status.fileStatus.forEach(file => {

console.log(`File: ${file.name}`);
});

// List branches

const response = await sandbox.git.branches("workspace/repo");

console.log(`Checked out branch: ${response.current}`);

response.branches.forEach(branch => {

console.log(`Branch: ${branch}`);
});

Branch operations

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 branches

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")
// Create new branch

await sandbox.git.createBranch('workspace/repo', 'new-feature');

Checkout branches or commits

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")
// Checkout a branch

await sandbox.git.checkoutBranch('workspace/repo', 'feature-branch');

// Checkout a commit (detached HEAD)

await sandbox.git.checkoutBranch('workspace/repo', 'abc123def456');

Delete branches

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")
// Delete a branch

await sandbox.git.deleteBranch('workspace/repo', 'old-feature');

Stage changes

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

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
)

Remote operations

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