Skip to content

Git Clone Commands

Overview

The git clone command is used to create a copy of an existing Git repository. It is commonly used to obtain a working copy of a repository hosted on a remote server. This document covers the basic and advanced usage of git clone.

Basic Clone

git clone <repository>

Clones a repository from the specified URL.

git clone <repository>

Example

git clone https://github.com/example/repo.git

This command clones the repository from the given URL into a directory named repo.

Cloning into a Specific Directory

git clone <repository> <directory>

Clones a repository into the specified directory.

git clone <repository> <directory>

Example

git clone https://github.com/example/repo.git my-repo

This command clones the repository into a directory named my-repo.

Cloning with Specific Branch

git clone -b <branch> <repository>

Clones a repository and checks out the specified branch.

git clone -b <branch> <repository>

Example

git clone -b feature-branch https://github.com/example/repo.git

This command clones the repository and checks out the feature-branch branch.

Cloning with Depth (Shallow Clone)

git clone --depth <depth> <repository>

Creates a shallow clone with a history truncated to the specified number of commits.

git clone --depth <depth> <repository>

Example

git clone --depth 1 https://github.com/example/repo.git

This command clones the repository with a history truncated to the most recent commit.

Cloning with SSH

git clone <ssh://repository>

Clones a repository using the SSH protocol.

git clone <ssh://repository>

Example

git clone git@github.com:example/repo.git

This command clones the repository using SSH authentication.

Cloning a Submodule

git clone --recurse-submodules <repository>

Clones a repository and initializes and updates any submodules.

git clone --recurse-submodules <repository>

Example

git clone --recurse-submodules https://github.com/example/repo.git

This command clones the repository and ensures all submodules are initialized and updated.

Cloning with Configuration

git clone -c <config>=<value> <repository>

Sets configuration values during the clone operation.

git clone -c <config>=<value> <repository>

Example

git clone -c core.autocrlf=input https://github.com/example/repo.git

This command clones the repository and sets the core.autocrlf configuration to input during the clone.

Summary

The git clone command is used to create a copy of an existing repository, with options to specify the directory, branch, depth, and more. It is essential for starting work on an existing project or contributing to an open-source project. For more detailed information on each command, refer to the official Git documentation.