LinuxCommandLibrary

git-clone

Copy a Git repository to your computer

TLDR

Clone an existing repository into a new directory (the default directory is the repository name)

$ git clone [remote_repository_location] [path/to/directory]
copy

Clone an existing repository and its submodules
$ git clone --recursive [remote_repository_location]
copy

Clone only the .git directory of an existing repository
$ git clone [[-n|--no-checkout]] [remote_repository_location]
copy

Clone a local repository
$ git clone [[-l|--local]] [path/to/local_repository]
copy

Clone quietly
$ git clone [[-q|--quiet]] [remote_repository_location]
copy

Clone an existing repository only fetching the 10 most recent commits on the default branch (useful to save time)
$ git clone --depth 10 [remote_repository_location]
copy

Clone an existing repository only fetching a specific branch
$ git clone [[-b|--branch]] [name] --single-branch [remote_repository_location]
copy

Clone an existing repository using a specific SSH command
$ git clone [[-c|--config]] core.sshCommand="[ssh -i path/to/private_ssh_key]" [remote_repository_location]
copy

SYNOPSIS

git clone [options] <repository> [<directory>]

PARAMETERS

--local, -l
    Optimized cloning for local repositories.

--no-hardlinks
    Copy instead of using hardlinks for local clones.

--shared, -s
    Setup shared repository object database.

--reference <repository>
    Use reference repository for objects.

--reference-if-able <repository>
    Use reference repository if possible.

--dissociate
    Convert reference clone to standalone.

--separate-git-dir <gitdir>
    Place gitdir file outside repository.

--quiet, -q
    Suppress progress output.

--verbose, -v
    Enable verbose output.

--progress
    Force progress reporting.

-n
    Do not create a working tree (bare-like).

--bare
    Clone into bare repository (no working tree).

--mirror
    Bare clone that mirrors all refs.

--origin <name>, -o
    Use <name> instead of 'origin' for remote.

--branch <name>, -b
    Checkout <name> branch after clone.

--upload-pack <exec>
    Specify path to git-upload-pack.

--template <directory>
    Use directory for initialization template.

--config <key>=<value>
    Set config key=value during clone.

--depth <depth>
    Create shallow clone with history depth.

--shallow-since <date>
    Shallow clone since specified date.

--shallow-exclude <revision>
    Shallow clone excluding revision.

--single-branch
    Clone only one branch (HEAD by default).

--no-single-branch
    Clone all branches (default).

--no-tags
    Do not fetch tags.

--recurse-submodules[=pathspec]
    Initialize and update submodules.

--jobs <n>, -j <n>
    Number of submodules cloned in parallel.

--filter <filter>
    Object filter for partial clone.

--also-filter-submodules
    Apply filter to submodules.

--remote-submodules
    Clone submodules with remote heads.

DESCRIPTION

git clone creates a local copy of a remote Git repository, downloading its entire history, files, branches, and tags. It initializes a new Git repository in a specified or default directory (named after the repo's basename), sets up remote tracking for the origin, and checks out the default branch (typically main or master).

Supports various protocols like HTTPS, SSH, Git, and local paths. By default, it performs a full clone, but options enable shallow clones for efficiency, bare repositories without working trees, or submodule recursion. Ideal for starting collaboration on projects hosted on platforms like GitHub, GitLab, or self-hosted servers.

During cloning, Git fetches objects via packfiles, verifies integrity, and configures remotes. Post-clone, users can git pull for updates or git push changes. Large repositories benefit from options like --depth to limit history depth, saving bandwidth and disk space.

CAVEATS

Cloning large repositories consumes significant disk space and time; use --depth or --filter for partial clones. Requires network access for remote URLs. Default behavior tracks all branches; specify --single-branch to limit.

COMMON EXAMPLES

git clone https://github.com/user/repo.git
git clone -b develop --single-branch https://... repo-dev
git clone --depth 1 --shallow-submodules https://... shallow-repo

REPOSITORY URLS

Supports https://, ssh://, git://, user@host:path, local paths like /path/to/repo or file:///path.

HISTORY

Introduced in Git 1.0.0 (2005) by Linus Torvalds as core command for repository duplication. Evolved with shallow cloning (v1.7.6, 2010), partial clones (v2.19, 2018), and submodule improvements over releases.

SEE ALSO

Copied to clipboard