Skip to main content

Synopsis

git clone [--template=<template-directory>]
          [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
          [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
          [--dissociate] [--separate-git-dir <git-dir>]
          [--depth <depth>] [--[no-]single-branch] [--no-tags]
          [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
          [--[no-]remote-submodules] [--jobs <n>] [--sparse] [--[no-]reject-shallow]
          [--filter=<filter-spec>] [--also-filter-submodules]] [--] <repository>
          [<directory>]

Description

Clones a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned repository, and creates and checks out an initial branch that is forked from the cloned repository’s currently active branch.
After the clone, a plain git fetch without arguments will update all the remote-tracking branches, and a git pull without arguments will in addition merge the remote default branch into the current branch.

Common Usage

1

Clone a repository

Clone a repository from GitHub:
git clone https://github.com/git/git.git
2

Clone into a specific directory

Specify the target directory name:
git clone https://github.com/git/git.git my-git
3

Clone a specific branch

Clone only a specific branch:
git clone -b main --single-branch https://github.com/git/git.git

Options

Create a shallow clone with a history truncated to the specified number of commits.
git clone --depth 1 https://github.com/git/git.git
Shallow clones are faster and use less disk space. Perfect for CI/CD environments.
Instead of pointing the newly created HEAD to the default branch, point it to the specified branch.
git clone -b develop https://github.com/user/repo.git
Clone only the history leading to the tip of a single branch.
git clone --single-branch --branch main https://github.com/user/repo.git
Instead of using the remote name origin, use the specified name.
git clone -o upstream https://github.com/git/git.git
Make a bare Git repository - without a working directory.
git clone --bare https://github.com/user/repo.git
Set up a mirror of the source repository - implies --bare.
git clone --mirror https://github.com/user/repo.git
Initialize and clone submodules within the repository.
git clone --recurse-submodules https://github.com/user/repo.git
Operate quietly - suppress progress reporting.
git clone --quiet https://github.com/user/repo.git

Examples

Clone from different protocols

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

Shallow clone for faster download

# Clone only the latest commit
git clone --depth 1 https://github.com/torvalds/linux.git

# Later, if you need the full history
cd linux
git fetch --unshallow

Clone with submodules

# Clone repository and all submodules
git clone --recurse-submodules https://github.com/user/repo.git

# Or clone first, then initialize submodules
git clone https://github.com/user/repo.git
cd repo
git submodule update --init --recursive
  • git init - Create an empty Git repository
  • git fetch - Download objects and refs from another repository
  • git pull - Fetch from and integrate with another repository