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

<repository>
    The URL or path to the remote repository to clone. This can be a local path, an HTTP(S) URL, an SSH URL, or a Git protocol URL.

[<directory>]
    The optional name of the directory into which the repository will be cloned. If not provided, Git will create a directory with the same name as the repository (minus the .git suffix).

-b <name>,
--branch <name>

    Instead of pointing the newly created HEAD to the branch pointed to by the cloned repository’s HEAD, point to the branch named <name>. In a non-bare repository, this is also the branch that is checked out.

--bare
    Make a bare Git repository. That is, rather than creating <directory> and placing the administrative files in <directory>/.git, make the <directory> itself the .git folder. This implies --no-checkout as no working tree is created.

--mirror
    Set up a mirror of the source repository. This implies --bare. When a mirror is fetched, all refs, and thus all branches and tags, are updated on the local side.

--depth <depth>
    Create a shallow clone with a history truncated to the specified number of commits. Useful for large repositories where you only need recent history.

-o <name>,
--origin <name>

    Instead of using the remote name 'origin' to keep track of the upstream repository, use <name>.

-q,
--quiet

    Operate quietly. Progress is not reported to the standard error stream.

-n,
--no-checkout

    No checkout of HEAD is performed after the clone is complete. The .git directory is initialized, but the working tree is left empty. Useful for server-side cloning where you don't need a working copy.

--single-branch
    Clone only the history of a single branch, even if the remote has multiple branches. Implies --no-tags unless --no-tags is specified.

--recurse-submodules[=<pathspec>]
    After the clone is created, initialize and clone submodules within it. If optional <pathspec> is given, only submodules whose path matches the pathspec are initialized and cloned.

--jobs <n>
    The number of submodules that should be fetched and checked out simultaneously. Defaults to the submodule.fetchJobs configuration option.

DESCRIPTION

The git clone command is used to create a local copy of an existing Git repository. When you clone a repository, Git downloads all versions of all files and folders for the entire project history, including all branches and tags. This creates a fully functional local Git repository, which includes a remote connection (usually named origin) pointing back to the original source repository. This local copy is then ready for you to start working on, allowing you to make changes, commit them, and push them back to the remote repository, or pull updates from it. It's the first step to contribute to an existing Git project or to get a local copy of your own remote repository.

CAVEATS

Cloning large repositories with deep histories can consume significant disk space and network bandwidth.
Be cautious when cloning from untrusted sources, as the repository might contain Git hooks that execute malicious code upon cloning or subsequent operations.
Shallow clones (using --depth) can sometimes cause issues if you later need to access older history or push changes that conflict with the truncated history. Converting a shallow clone to a full clone requires fetching missing history.

DEFAULT BEHAVIOR

By default, git clone fetches all branches and tags from the remote repository, creates a local master (or main, depending on the remote's default branch name) branch, and sets up a remote tracking branch for it, usually named origin/master or origin/main. It then checks out the content of the default branch into your working directory, making it ready for immediate use.

PROTOCOL SUPPORT

git clone supports various protocols, including HTTP/HTTPS, SSH, Git protocol (git://), and local filesystem paths. The choice of protocol often depends on authentication requirements and network configuration.

HISTORY

The git clone command is one of the foundational commands of Git, present since its very first public release in 2005. It was designed to be simple and efficient, creating a complete, fully independent local copy of a remote repository. Over time, options like --depth (for shallow clones) and --recurse-submodules were added to address specific use cases like large repositories and projects with nested dependencies, enhancing its utility for a wider range of development workflows.

SEE ALSO

Copied to clipboard