# Git (/docs/go-sdk/git)

> For AI agents: the complete documentation index is at https://www.daytona.com/llms.txt.

## Git [#git]

```go
type Git struct {
	// contains filtered or unexported fields
}
```

Git provides sandbox repository operations. Remote paths and pathspecs are
passed to Git unchanged, including relative paths. Obtain it with Sandbox.Git.
Server failures retain codes such as ErrGitAuthFailed, ErrGitRepoNotFound,
ErrGitBranchNotFound and ErrGitBranchExists.

#### Git.Add [#gitadd]

```go
func (g *Git) Add(ctx context.Context, path string, files []string) error
```

Add stages string pathspecs. Nil files is sent as an empty array, never null.

#### Git.AddRemote [#gitaddremote]

```go
func (g *Git) AddRemote(ctx context.Context, path, name, url string) error
```

AddRemote adds a named remote at url.

#### Git.CheckoutBranch [#gitcheckoutbranch]

```go
func (g *Git) CheckoutBranch(ctx context.Context, path, branch string) error
```

CheckoutBranch checks out an existing branch; missing branches return ErrGitBranchNotFound.

#### Git.Clone [#gitclone]

```go
func (g *Git) Clone(ctx context.Context, url, path string, opts *GitCloneOptions) error
```

Clone clones url into path. Depth and Timeout are validated locally.

**Example**:

```go
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
client, err := daytona.New()
if err != nil {
	log.Fatal(err)
}
defer func() {
	if closeErr := client.Close(); closeErr != nil {
		log.Print(closeErr)
	}
}()
sandbox, err := client.Get(ctx, "sandbox-id", nil)
if err != nil {
	log.Fatal(err)
}
// For token authentication, also set Username: "git", Password: token.
err = sandbox.Git().Clone(ctx, "https://github.com/octocat/Hello-World", "/home/daytona/repo",
	&daytona.GitCloneOptions{Depth: 1})
if err != nil {
	log.Fatal(err)
}
```

#### Git.Commit [#gitcommit]

```go
func (g *Git) Commit(ctx context.Context, path, message string, opts *GitCommitOptions) (*GitCommitResponse, error)
```

Commit creates a commit and returns its captured hash (not a subsequent HEAD
lookup). Author and Email are required; invalid input returns ErrBadRequest.

**Example**:

```go
ctx := context.Background()
client, err := daytona.New()
if err != nil {
	log.Fatal(err)
}
defer func() {
	if closeErr := client.Close(); closeErr != nil {
		log.Print(closeErr)
	}
}()
sandbox, err := client.Get(ctx, "sandbox-id", nil)
if err != nil {
	log.Fatal(err)
}
git := sandbox.Git()
if addErr := git.Add(ctx, "/home/daytona/repo", []string{"README"}); addErr != nil {
	log.Fatal(addErr)
}
commit, err := git.Commit(ctx, "/home/daytona/repo", "Update README", &daytona.GitCommitOptions{
	Author: "SDK User", Email: "sdk@example.org",
})
if err != nil {
	log.Fatal(err)
}
fmt.Println(commit.Hash)
```

#### Git.CreateBranch [#gitcreatebranch]

```go
func (g *Git) CreateBranch(ctx context.Context, path, name string) error
```

CreateBranch creates name; ErrGitBranchExists identifies a duplicate.

#### Git.DeleteBranch [#gitdeletebranch]

```go
func (g *Git) DeleteBranch(ctx context.Context, path, name string, opts *GitDeleteBranchOptions) error
```

DeleteBranch safely deletes a merged branch by default. An unmerged or
checked-out branch returns ErrConflict (HTTP 409). Force allows deleting an
unmerged branch, never the checked-out one. Missing names return ErrGitBranchNotFound.

#### Git.GetConfig [#gitgetconfig]

```go
func (g *Git) GetConfig(ctx context.Context, key string, opts *GitConfigOptions) (*string, error)
```

GetConfig returns nil when key is unset (distinct from an empty string).

#### Git.History [#githistory]

```go
func (g *Git) History(ctx context.Context, path string) (*Page[GitCommit], error)
```

History returns commits newest first, accepting both array and envelope
responses. It makes exactly one request, retaining an optional next cursor.

#### Git.Init [#gitinit]

```go
func (g *Git) Init(ctx context.Context, path string, opts *GitInitOptions) error
```

Init initializes a repository; nil opts uses Git's defaults.

#### Git.ListBranches [#gitlistbranches]

```go
func (g *Git) ListBranches(ctx context.Context, path string) (*Page[string], error)
```

ListBranches returns the repository's branch names in a single page.

#### Git.ListRemotes [#gitlistremotes]

```go
func (g *Git) ListRemotes(ctx context.Context, path string) (*Page[GitRemote], error)
```

ListRemotes returns configured remotes. The server strips credentials from URLs.

#### Git.Pull [#gitpull]

```go
func (g *Git) Pull(ctx context.Context, path string, opts *GitCredentialOptions) error
```

Pull pulls from the named or configured remote, with a default 30-minute total deadline.

#### Git.Push [#gitpush]

```go
func (g *Git) Push(ctx context.Context, path string, opts *GitPushOptions) error
```

Push pushes to the named or configured remote, with a default 30-minute total deadline.

#### Git.Reset [#gitreset]

```go
func (g *Git) Reset(ctx context.Context, path string, opts *GitResetOptions) error
```

Reset resets repository state; nil opts uses a mixed reset to HEAD.

#### Git.Restore [#gitrestore]

```go
func (g *Git) Restore(ctx context.Context, path string, files []string, opts *GitRestoreOptions) error
```

Restore restores pathspecs from the index or Source. Nil opts uses Git's defaults.

#### Git.SetConfig [#gitsetconfig]

```go
func (g *Git) SetConfig(ctx context.Context, key, value string, opts *GitConfigOptions) error
```

SetConfig writes a configuration value. A sandbox access token requires both
toolbox:git and toolbox:exec (toolbox covers both). Without toolbox:exec it
returns ErrForbidden, although GetConfig still works.

#### Git.Status [#gitstatus]

```go
func (g *Git) Status(ctx context.Context, path string) (*GitStatus, error)
```

Status returns the current branch, tracking counts and changed files.

## GitCloneOptions [#gitcloneoptions]

```go
type GitCloneOptions struct {
	// Branch selects a branch when CommitID is absent.
	Branch string
	// CommitID detaches the clone at this commit.
	CommitID string
	// Depth selects a positive shallow depth; zero omits it.
	Depth int
	// InsecureSkipTLS disables certificate checks only for this clone.
	InsecureSkipTLS bool
	// Username authenticates to the remote.
	Username string
	// Password authenticates to the remote. For a personal access token, set
	// Username to "git" and Password to the token.
	Password string
	// Timeout replaces the total request deadline; zero defaults to 30 minutes,
	// NoTimeout disables it. The caller context always bounds the request.
	Timeout time.Duration
}
```

GitCloneOptions configures a clone. Nil options uses a full clone and a
30-minute request deadline, independently of the client's request timeout.

#### GitCloneOptions.GoString [#gitcloneoptionsgostring]

```go
func (o GitCloneOptions) GoString() string
```

GoString prints clone options with the same redaction as String.
The %#v format uses the redacted %+v form, not Go syntax.

#### GitCloneOptions.String [#gitcloneoptionsstring]

```go
func (o GitCloneOptions) String() string
```

String prints all clone options with a non-empty Password redacted.

## GitCommit [#gitcommit-1]

```go
type GitCommit struct {
	// Hash is the full commit SHA.
	Hash string
	// Message is the commit message.
	Message string
	// Author is the author name.
	Author string
	// Email is the author email.
	Email string
	// Timestamp is the author timestamp.
	Timestamp time.Time
}
```

GitCommit is a commit history entry.

## GitCommitOptions [#gitcommitoptions]

```go
type GitCommitOptions struct {
	// Author is the required non-blank author name.
	Author string
	// Email is the required non-blank author email.
	Email string
	// AllowEmpty permits a commit without staged changes; default false.
	AllowEmpty bool
}
```

GitCommitOptions supplies the required identity for this commit only.

## GitCommitResponse [#gitcommitresponse]

```go
type GitCommitResponse struct {
	// Hash is the created commit's full SHA.
	Hash string
}
```

GitCommitResponse identifies the commit created by Commit.

## GitConfigOptions [#gitconfigoptions]

```go
type GitConfigOptions struct {
	// Path is the repository path, needed for local scope.
	Path string
	// Scope defaults to GitConfigScopeGlobal.
	Scope GitConfigScope
}
```

GitConfigOptions selects the Git configuration location.

## GitConfigScope [#gitconfigscope]

```go
type GitConfigScope string
```

GitConfigScope selects a global or repository-local Git configuration.

```go
const (
	// GitConfigScopeGlobal selects the user's global configuration (the default).
	GitConfigScopeGlobal GitConfigScope = "global"
	// GitConfigScopeLocal selects the repository configuration.
	GitConfigScopeLocal GitConfigScope = "local"
)
```

## GitCredentialOptions [#gitcredentialoptions]

```go
type GitCredentialOptions struct {
	// Username authenticates to the remote.
	Username string
	// Password authenticates to the remote. For a personal access token, set
	// Username to "git" and Password to the token.
	Password string
	// Branch selects a remote branch; empty preserves Git's default behavior.
	Branch string
	// Remote selects a named remote; empty preserves Git's default behavior.
	Remote string
	// Timeout replaces the total request deadline; zero defaults to 30 minutes,
	// NoTimeout disables it. Context cancellation remains effective.
	Timeout time.Duration
}
```

GitCredentialOptions selects a remote and its authentication.

#### GitCredentialOptions.GoString [#gitcredentialoptionsgostring]

```go
func (o GitCredentialOptions) GoString() string
```

GoString prints credential options with the same redaction as String.
The %#v format uses the redacted %+v form, not Go syntax.

#### GitCredentialOptions.String [#gitcredentialoptionsstring]

```go
func (o GitCredentialOptions) String() string
```

String prints all credential options with a non-empty Password redacted.

## GitDeleteBranchOptions [#gitdeletebranchoptions]

```go
type GitDeleteBranchOptions struct {
	// Force deletes even an unmerged branch; default false.
	Force bool
}
```

GitDeleteBranchOptions controls safe versus forced branch deletion.

## GitFileStatus [#gitfilestatus]

```go
type GitFileStatus struct {
	// Name is the file path.
	Name string
	// Staging is Git's index status code.
	Staging string
	// Worktree is Git's working tree status code.
	Worktree string
	// Extra contains the additional path information reported by Git.
	Extra string
}
```

GitFileStatus describes one path in the index and working tree.

## GitInitOptions [#gitinitoptions]

```go
type GitInitOptions struct {
	// Bare creates a bare repository; default false.
	Bare bool
	// InitialBranch selects the initial branch; empty uses Git's default.
	InitialBranch string
}
```

GitInitOptions configures a new repository.

## GitPushOptions [#gitpushoptions]

```go
type GitPushOptions struct {
	// GitCredentialOptions selects credentials, branch, remote and timeout.
	GitCredentialOptions
	// SetUpstream sets upstream tracking; default false.
	SetUpstream bool
}
```

GitPushOptions configures push credentials and upstream tracking.

#### GitPushOptions.GoString [#gitpushoptionsgostring]

```go
func (o GitPushOptions) GoString() string
```

GoString prints push options with the same redaction as String.
The %#v format uses the redacted %+v form, not Go syntax.

#### GitPushOptions.String [#gitpushoptionsstring]

```go
func (o GitPushOptions) String() string
```

String prints all push options with a non-empty Password redacted.

## GitRemote [#gitremote]

```go
type GitRemote struct {
	// Name identifies the remote.
	Name string
	// URL is the remote URL, with credentials removed by the server.
	URL string
}
```

GitRemote is a configured repository remote.

## GitResetMode [#gitresetmode]

```go
type GitResetMode string
```

GitResetMode selects how Git.Reset updates repository state.
The zero value leaves Git's mixed-reset default; other strings are passed
through for server validation, including future modes.

```go
const (
	// GitResetModeMixed resets the index but preserves the working tree.
	GitResetModeMixed GitResetMode = "mixed"
	// GitResetModeSoft preserves the index and working tree.
	GitResetModeSoft GitResetMode = "soft"
	// GitResetModeHard resets the index and working tree, discarding changes.
	GitResetModeHard GitResetMode = "hard"
	// GitResetModeMerge preserves unstaged changes where possible.
	GitResetModeMerge GitResetMode = "merge"
	// GitResetModeKeep refuses to overwrite local changes.
	GitResetModeKeep GitResetMode = "keep"
)
```

## GitResetOptions [#gitresetoptions]

```go
type GitResetOptions struct {
	// Mode selects mixed, soft, hard, merge or keep; empty uses Git's default.
	Mode GitResetMode
	// Target is the commit-ish; empty uses Git's default.
	Target string
	// Files contains relative pathspecs; nil omits the restriction.
	Files []string
}
```

GitResetOptions selects the reset operation.

## GitRestoreOptions [#gitrestoreoptions]

```go
type GitRestoreOptions struct {
	// Staged restores the index when true; nil leaves Git's default.
	Staged *bool
	// Worktree restores working files; nil leaves Git's default.
	Worktree *bool
	// Source is an optional source tree-ish.
	Source string
}
```

GitRestoreOptions selects the source and destinations of a restore.

## GitStatus [#gitstatus-1]

```go
type GitStatus struct {
	// CurrentBranch is the checked-out branch.
	CurrentBranch string
	// Ahead counts commits ahead of upstream.
	Ahead int64
	// Behind counts commits behind upstream.
	Behind int64
	// BranchPublished reports whether the branch has an upstream.
	BranchPublished bool
	// FileStatus contains the repository's changed files.
	FileStatus []GitFileStatus
}
```

GitStatus describes branch tracking and working tree changes.
