GitRepository
The GitRepository type represents a Git repository.
Common operations​
Some of the common operations used on the GitRepository type include:
| Field | Description | 
|---|---|
branch | Returns details of a branch | 
commit | Returns details of a commit | 
head | Returns details of the current HEAD | 
ref | Returns details of a ref | 
tag | Returns details of a tag | 
tags | Returns tags that match a given pattern | 
branches | Returns branches that match a given pattern | 
Example​
Clone a remote Git repository into a container​
The following Dagger Function accepts a Git repository URL and a Git reference. It copies the repository at the specified reference to the /src path in a container and returns the modified container.
For examples of SSH-based cloning, including private or public Git repositories with an SSH reference format, select the SSH tabs below. This approach requires explicitly forwarding the host SSH_AUTH_SOCK to the Dagger Function. Learn more about this in Dagger's sandboxed runtime model.
- Go
 - Go (SSH)
 - Python
 - Python (SSH)
 - TypeScript
 - TypeScript (SSH)
 - PHP
 - PHP (SSH)
 
package main
import (
	"context"
	"dagger/my-module/internal/dagger"
)
// Demonstrates cloning a Git repository over HTTP(S).
//
// For SSH usage, see the SSH snippet (CloneWithSsh).
type MyModule struct{}
func (m *MyModule) Clone(ctx context.Context, repository string, ref string) *dagger.Container {
	d := dag.Git(repository).Ref(ref).Tree()
	return dag.Container().
		From("alpine:latest").
		WithDirectory("/src", d).
		WithWorkdir("/src")
}
package main
import (
	"context"
	"dagger/my-module/internal/dagger"
)
// Demonstrates an SSH-based clone requiring a user-supplied SSHAuthSocket.
type MyModule struct{}
func (m *MyModule) CloneWithSsh(ctx context.Context, repository string, ref string, sock *dagger.Socket) *dagger.Container {
	d := dag.Git(repository, dagger.GitOpts{SSHAuthSocket: sock}).Ref(ref).Tree()
	return dag.Container().
		From("alpine:latest").
		WithDirectory("/src", d).
		WithWorkdir("/src")
}
import dagger
from dagger import dag, function, object_type
@object_type
class MyModule:
    """
    Demonstrates cloning a Git repository over HTTP(S).
    For SSH usage, see the SSH snippet (clone_with_ssh).
    """
    @function
    async def clone(
        self,
        repository: str,
        ref: str,
    ) -> dagger.Container:
        repo_dir = dag.git(repository).ref(ref).tree()
        return (
            dag.container()
            .from_("alpine:latest")
            .with_directory("/src", repo_dir)
            .with_workdir("/src")
        )
import dagger
from dagger import dag, function, object_type
@object_type
class MyModule:
    """Demonstrates an SSH-based clone requiring a user-supplied ssh_auth_socket."""
    @function
    async def clone_with_ssh(
        self, repository: str, ref: str, sock: dagger.Socket
    ) -> dagger.Container:
        repo_dir = dag.git(repository, ssh_auth_socket=sock).ref(ref).tree()
        return (
            dag.container()
            .from_("alpine:latest")
            .with_directory("/src", repo_dir)
            .with_workdir("/src")
        )
import { dag, Directory, Container, object, func } from "@dagger.io/dagger"
@object()
class MyModule {
  /**
    Demonstrates cloning a Git repository over HTTP(S).
    For SSH usage, see the SSH snippet (cloneWithSsh).
   */
  @func()
  clone(repository: string, ref: string): Container {
    const repoDir = dag.git(repository, { sshAuthSocket: sock }).ref(ref).tree()
    return dag
      .container()
      .from("alpine:latest")
      .withDirectory("/src", repoDir)
      .withWorkdir("/src")
  }
}
import { dag, Container, object, func, Socket } from "@dagger.io/dagger"
@object()
class MyModule {
  /**
    Demonstrates an SSH-based clone requiring a user-supplied sshAuthSocket.
   */
  @func()
  cloneWithSsh(repository: string, ref: string, sock: Socket): Container {
    const repoDir = dag.git(repository, { sshAuthSocket: sock }).ref(ref).tree()
    return dag
      .container()
      .from("alpine:latest")
      .withDirectory("/src", repoDir)
      .withWorkdir("/src")
  }
}
<?php
declare(strict_types=1);
namespace DaggerModule;
use Dagger\Attribute\DaggerFunction;
use Dagger\Attribute\DaggerObject;
use Dagger\Container;
use function Dagger\dag;
#[DaggerObject]
class MyModule
{
    #[DaggerFunction]
    public function clone(string $repository, string $ref): Container
    {
        $repoDir = dag()->git($repository)->ref($ref)->tree();
        return dag()
            ->container()
            ->from('alpine:latest')
            ->withDirectory('/src', $repoDir);
    }
}
<?php
declare(strict_types=1);
namespace DaggerModule;
use Dagger\Attribute\DaggerFunction;
use Dagger\Attribute\DaggerObject;
use Dagger\Attribute\Doc;
use Dagger\Container;
use Dagger\Socket;
use function Dagger\dag;
#[DaggerObject]
class MyModule
{
    #[DaggerFunction]
    #[Doc('Demonstrates an SSH-based clone requiring a user-supplied sshAuthSocket.')]
    public function cloneWithSsh(string $repository, string $ref, Socket $sock): Container
    {
        $repoDir = dag()
            ->git($repository, true, '', $sock)
            ->ref($ref)
            ->tree();
        return dag()
            ->container()
            ->from('alpine:latest')
            ->withDirectory('/src', $repoDir)
            ->withWorkdir('/src');
    }
}
Examples
Clone the public dagger/dagger GitHub repository to /src in the container:
- System shell
 - Dagger Shell
 - Dagger CLI
 
dagger -c 'clone https://github.com/dagger/dagger 196f232a4d6b2d1d3db5f5e040cf20b6a76a76c5'
clone https://github.com/dagger/dagger 196f232a4d6b2d1d3db5f5e040cf20b6a76a76c5
dagger call clone --repository=https://github.com/dagger/dagger --ref=196f232a4d6b2d1d3db5f5e040cf20b6a76a76c5
Clone the public dagger/dagger GitHub repository at reference 196f232a4d6b2d1d3db5f5e040cf20b6a76a76c5 to /src in the container and open an interactive terminal to inspect the container filesystem:
- System shell
 - Dagger Shell
 - Dagger CLI
 
dagger <<EOF
clone https://github.com/dagger/dagger 196f232a4d6b2d1d3db5f5e040cf20b6a76a76c5 |
  terminal
EOF
clone https://github.com/dagger/dagger 196f232a4d6b2d1d3db5f5e040cf20b6a76a76c5 | terminal
dagger call \
  clone --repository=https://github.com/dagger/dagger --ref=196f232a4d6b2d1d3db5f5e040cf20b6a76a76c5 \
  terminal
Clone over SSH with socket forwarding:
- System shell
 - Dagger Shell
 - Dagger CLI
 
dagger <<EOF
clone-with-ssh git@github.com:dagger/dagger.git 196f232a4d6b2d1d3db5f5e040cf20b6a76a76c5 $SSH_AUTH_SOCK |
  terminal
EOF
clone-with-ssh git@github.com:dagger/dagger.git 196f232a4d6b2d1d3db5f5e040cf20b6a76a76c5 $SSH_AUTH_SOCK | terminal
dagger call \
  clone-with-ssh --repository=git@github.com:dagger/dagger.git --ref=196f232a4d6b2d1d3db5f5e040cf20b6a76a76c5 --sock=$SSH_AUTH_SOCK \
  terminal