Git Operations
The Daytona SDK provides built-in Git support. This guide covers all available Git
operations and best practices. Daytona SDK provides an option to clone, check status,
and manage Git repositories in Sandboxes. You can interact with Git repositories using
the git
module.
Example:
Basic Git workflow:
sandbox = daytona.create()
# Clone a repositorysandbox.git.clone( url="https://github.com/user/repo.git", path="/workspace/repo")
# Make some changessandbox.fs.upload_file("/workspace/repo/test.txt", "Hello, World!")
# Stage and commit changessandbox.git.add("/workspace/repo", ["test.txt"])sandbox.git.commit( path="/workspace/repo", message="Add test file", author="John Doe", email="john@example.com")
# Push changes (with authentication)sandbox.git.push( path="/workspace/repo", username="user", password="token")
Notes:
All paths should be absolute paths within the Sandbox if not explicitly stated otherwise.
Git
class Git()
Provides Git operations within a Sandbox.
This class implements a high-level interface to Git operations that can be performed within a Daytona Sandbox. It supports common Git operations like cloning repositories, staging and committing changes, pushing and pulling changes, and checking repository status.
Attributes:
sandbox
Sandbox - The parent Sandbox instance.instance
SandboxInstance - The Sandbox instance this Git handler belongs to.
Example:
# Clone a repositorysandbox.git.clone( url="https://github.com/user/repo.git", path="/workspace/repo")
# Check repository statusstatus = sandbox.git.status("/workspace/repo")print(f"Modified files: {status.modified}")
# Stage and commit changessandbox.git.add("/workspace/repo", ["file.txt"])sandbox.git.commit( path="/workspace/repo", message="Update file", author="John Doe", email="john@example.com")
Git.__init__
def __init__(sandbox: "Sandbox", toolbox_api: ToolboxApi, instance: SandboxInstance)
Initializes a new Git handler instance.
Arguments:
sandbox
Sandbox - The parent Sandbox instance.toolbox_api
ToolboxApi - API client for Sandbox operations.instance
SandboxInstance - The Sandbox instance this Git handler belongs to.
Git.add
@intercept_errors(message_prefix="Failed to add files: ")def add(path: str, files: List[str]) -> None
Stages files for commit.
This method stages the specified files for the next commit, similar to running ‘git add’ on the command line.
Arguments:
path
str - Absolute path to the Git repository root.files
List[str] - List of file paths or directories to stage, relative to the repository root.
Example:
# Stage a single filesandbox.git.add("/workspace/repo", ["file.txt"])
# Stage multiple filessandbox.git.add("/workspace/repo", [ "src/main.py", "tests/test_main.py", "README.md"])
Git.branches
@intercept_errors(message_prefix="Failed to list branches: ")def branches(path: str) -> ListBranchResponse
Lists branches in the repository.
This method returns information about all branches in the repository.
Arguments:
path
str - Absolute path to the Git repository root.
Returns:
ListBranchResponse
- List of branches in the repository.
Example:
response = sandbox.git.branches("/workspace/repo")print(f"Branches: {response.branches}")
Git.clone
@intercept_errors(message_prefix="Failed to clone repository: ")def clone(url: str, path: str, branch: Optional[str] = None, commit_id: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None) -> None
Clones a Git repository.
This method clones a Git repository into the specified path. It supports cloning specific branches or commits, and can authenticate with the remote repository if credentials are provided.
Arguments:
url
str - Repository URL to clone from.path
str - Absolute path where the repository should be cloned.branch
Optional[str] - Specific branch to clone. If not specified, clones the default branch.commit_id
Optional[str] - Specific commit to clone. If specified, the repository will be left in a detached HEAD state at this commit.username
Optional[str] - Git username for authentication.password
Optional[str] - Git password or token for authentication.
Example:
# Clone the default branchsandbox.git.clone( url="https://github.com/user/repo.git", path="/workspace/repo")
# Clone a specific branch with authenticationsandbox.git.clone( url="https://github.com/user/private-repo.git", path="/workspace/private", branch="develop", username="user", password="token")
# Clone a specific commitsandbox.git.clone( url="https://github.com/user/repo.git", path="/workspace/repo-old", commit_id="abc123")
Git.commit
@intercept_errors(message_prefix="Failed to commit changes: ")def commit(path: str, message: str, author: str, email: str) -> None
Commits staged changes.
This method creates a new commit with the staged changes. Make sure to stage changes using the add() method before committing.
Arguments:
path
str - Absolute path to the Git repository root.message
str - Commit message describing the changes.author
str - Name of the commit author.email
str - Email address of the commit author.
Example:
# Stage and commit changessandbox.git.add("/workspace/repo", ["README.md"])sandbox.git.commit( path="/workspace/repo", message="Update documentation", author="John Doe", email="john@example.com")
Git.push
@intercept_errors(message_prefix="Failed to push changes: ")def push(path: str, username: Optional[str] = None, password: Optional[str] = None) -> None
Pushes local commits to the remote repository.
This method pushes all local commits on the current branch to the remote repository. If the remote repository requires authentication, provide username and password/token.
Arguments:
path
str - Absolute path to the Git repository root.username
Optional[str] - Git username for authentication.password
Optional[str] - Git password or token for authentication.
Example:
# Push without authentication (for public repos or SSH)sandbox.git.push("/workspace/repo")
# Push with authenticationsandbox.git.push( path="/workspace/repo", username="user", password="github_token")
Git.pull
@intercept_errors(message_prefix="Failed to pull changes: ")def pull(path: str, username: Optional[str] = None, password: Optional[str] = None) -> None
Pulls changes from the remote repository.
This method fetches and merges changes from the remote repository into the current branch. If the remote repository requires authentication, provide username and password/token.
Arguments:
path
str - Absolute path to the Git repository root.username
Optional[str] - Git username for authentication.password
Optional[str] - Git password or token for authentication.
Example:
# Pull without authenticationsandbox.git.pull("/workspace/repo")
# Pull with authenticationsandbox.git.pull( path="/workspace/repo", username="user", password="github_token")
Git.status
@intercept_errors(message_prefix="Failed to get status: ")def status(path: str) -> GitStatus
Gets the current Git repository status.
This method returns detailed information about the current state of the repository, including staged, unstaged, and untracked files.
Arguments:
path
str - Absolute path to the Git repository root.
Returns:
GitStatus
- Repository status information including:- current_branch: Current branch name
- file_status: List of file statuses
- ahead: Number of local commits not pushed to remote
- behind: Number of remote commits not pulled locally
- branch_published: Whether the branch has been published to the remote repository
Example:
status = sandbox.git.status("/workspace/repo")print(f"On branch: {status.current_branch}")print(f"Commits ahead: {status.ahead}")print(f"Commits behind: {status.behind}")
The Daytona SDK provides built-in Git support. This guide covers all available Git
operations and best practices. Daytona SDK provides an option to clone, check status,
and manage Git repositories in Sandboxes. You can interact with Git repositories using
the git
module.
Example:
Basic Git workflow:
sandbox = daytona.create()
# Clone a repositorysandbox.git.clone( url="https://github.com/user/repo.git", path="/workspace/repo")
# Make some changessandbox.fs.upload_file("/workspace/repo/test.txt", "Hello, World!")
# Stage and commit changessandbox.git.add("/workspace/repo", ["test.txt"])sandbox.git.commit( path="/workspace/repo", message="Add test file", author="John Doe", email="john@example.com")
# Push changes (with authentication)sandbox.git.push( path="/workspace/repo", username="user", password="token")
Notes:
All paths should be absolute paths within the Sandbox if not explicitly stated otherwise.