Skip to content

Git Operations

The Daytona SDK provides built-in Git support through the git module in Workspaces. This guide covers all available Git operations and best practices.

Basic Operations

Daytona SDK provides an option to clone, check status, and manage Git repositories in Workspaces. You can interact with Git repositories using the git module.

Cloning Repositories

Daytona SDK provides an option to clone Git repositories into Workspaces. You can clone public or private repositories, specific branches, and authenticate using personal access tokens.

Python

# Basic clone
workspace.git.clone(
url="https://github.com/user/repo.git",
path="/workspace/repo"
)
# Clone with authentication
workspace.git.clone(
url="https://github.com/user/repo.git",
path="/workspace/repo",
username="git",
password="personal_access_token"
)
# Clone specific branch
workspace.git.clone(
url="https://github.com/user/repo.git",
path="/workspace/repo",
branch="develop"
)

TypeScript

// Basic clone
await workspace.git.clone(
"https://github.com/user/repo.git",
"/workspace/repo"
);
// Clone with authentication
await workspace.git.clone(
"https://github.com/user/repo.git",
"/workspace/repo",
undefined,
undefined,
"git",
"personal_access_token"
);
// Clone specific branch
await workspace.git.clone(
"https://github.com/user/repo.git",
"/workspace/repo",
"develop"
);

Repository Status

Daytona SDK provides an option to check the status of Git repositories in Workspaces. You can get the current branch, modified files, staged files, and untracked files.

Python

# Get repository status
status = workspace.git.status("/workspace/repo")
print(f"Current branch: {status.current_branch}")
print(f"Modified files: {status.modified}")
print(f"Staged files: {status.staged}")
print(f"Untracked files: {status.untracked}")
# Get current branch
branch = workspace.git.current_branch("/workspace/repo")
print(f"Current branch: {branch}")
# List branches
branches = workspace.git.list_branches("/workspace/repo")
for branch in branches:
print(f"Branch: {branch.name} {'(current)' if branch.current else ''}")

TypeScript

// Get repository status
const status = await workspace.git.status("/workspace/repo");
console.log(`Current branch: ${status.currentBranch}`);
console.log(`Modified files: ${status.modified}`);
console.log(`Staged files: ${status.staged}`);
console.log(`Untracked files: ${status.untracked}`);
// Get current branch
const branch = await workspace.git.currentBranch("/workspace/repo");
console.log(`Current branch: ${branch}`);
// List branches
const branches = await workspace.git.listBranches("/workspace/repo");
branches.forEach(branch => {
console.log(`Branch: ${branch.name} ${branch.current ? '(current)' : ''}`);
});

Branch Operations

Daytona SDK provides an option to manage branches in Git repositories. You can create, switch, and delete branches.

Managing Branches

Python

# Create new branch
workspace.git.create_branch("/workspace/repo", "feature/new-feature")
# Switch branch
workspace.git.checkout("/workspace/repo", "feature/new-feature")
# Delete branch
workspace.git.delete_branch("/workspace/repo", "feature/old-feature")

TypeScript

// Create new branch
await workspace.git.createBranch("/workspace/repo", "feature/new-feature");
// Switch branch
await workspace.git.checkout("/workspace/repo", "feature/new-feature");
// Delete branch
await workspace.git.deleteBranch("/workspace/repo", "feature/old-feature");

Staging and Committing

Daytona SDK provides an option to stage and commit changes in Git repositories. You can stage specific files, all changes, and commit with a message.

Working with Changes

Python

# Stage specific files
workspace.git.add("/workspace/repo", ["file1.txt", "file2.txt"])
# Stage all changes
workspace.git.add("/workspace/repo", ["."])
# Commit changes
workspace.git.commit("/workspace/repo", "feat: add new feature")
# Get commit history
commits = workspace.git.log("/workspace/repo")
for commit in commits:
print(f"Commit: {commit.hash}")
print(f"Author: {commit.author}")
print(f"Message: {commit.message}")

TypeScript

// Stage specific files
await workspace.git.add("/workspace/repo", ["file1.txt", "file2.txt"]);
// Stage all changes
await workspace.git.add("/workspace/repo", ["."]);
// Commit changes
await workspace.git.commit("/workspace/repo", "feat: add new feature");
// Get commit history
const commits = await workspace.git.log("/workspace/repo");
commits.forEach(commit => {
console.log(`Commit: ${commit.hash}`);
console.log(`Author: ${commit.author}`);
console.log(`Message: ${commit.message}`);
});

Remote Operations

Daytona SDK provides an option to work with remote repositories in Git. You can push changes, pull changes, and list remotes.

Working with Remotes

Python

# Push changes
workspace.git.push("/workspace/repo")
# Pull changes
workspace.git.pull("/workspace/repo")
# List remotes
remotes = workspace.git.list_remotes("/workspace/repo")
for remote in remotes:
print(f"Remote: {remote.name} URL: {remote.url}")

TypeScript

// Push changes
await workspace.git.push("/workspace/repo");
// Push to specific remote and branch
await workspace.git.push("/workspace/repo", "origin", "feature/new-feature");
// Pull changes
await workspace.git.pull("/workspace/repo");
// Pull from specific remote and branch
await workspace.git.pull("/workspace/repo", "origin", "main");