Coding

Git Clone Command: Syntax, Options and Examples

git clone <url> copies a remote repository, with its full history, into a new folder on your computer and checks out the default branch. For example, git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY creates a folder named YOUR-REPOSITORY in the current directory.

Basic syntax

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

<repository> is the URL or path of the repository to copy. <directory> is optional. If you leave it out, Git uses the "humanish" part of the repository name: repo for /path/to/repo.git.

git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY my-folder

The second command puts the copy in my-folder. Cloning into an existing directory is allowed only if that directory is empty.

How to clone a GitHub repository

  1. Open the repository's main page on GitHub and click Code.
  2. Choose HTTPS or SSH and click the copy icon next to the URL.
  3. Open Terminal (macOS, Linux) or Git Bash (Windows) and change to the folder where the copy should go.
  4. Type git clone, paste the URL, and press Enter.

An SSH clone needs an SSH key registered with your account. If it fails with a publickey error, see how to add an SSH key to GitHub and how to fix "Permission denied (publickey)".

URL formats git clone accepts

FormSyntax
HTTPShttps://host[:port]/path-to-git-repo
SSHssh://[user@]host[:port]/path-to-git-repo
SSH, scp-like[user@]host:/path-to-git-repo
Git protocolgit://host[:port]/path-to-git-repo
Local path/path/to/repo.git/ (implies --local)
Local file URLfile:///path/to/repo.git/ (does not imply --local)

The scp-like form is recognised only when there is no slash before the first colon. To clone a local path that contains a colon, write it as ./foo:bar. The documentation also lists FTP and FTPS URLs, but calls them inefficient and deprecated and says not to use them.

Clone a specific branch

git clone --branch develop https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

-b (--branch) points the new HEAD at that branch instead of the one the remote's HEAD points to, and checks it out. This still fetches the other branches. To fetch only that branch's history, add --single-branch:

git clone --branch develop --single-branch https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

Later fetches then update only that branch's remote-tracking ref.

Clone a specific tag

git clone --branch v1.2.0 https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

--branch also accepts a tag. You end up in a detached HEAD at that commit, which is expected. See what a detached HEAD means before committing there.

Shallow clone: only the latest commits

git clone --depth 1 https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

--depth <depth> truncates history to that many commits. It implies --single-branch unless you pass --no-single-branch. This is useful for build servers or when you only need the current files, and you can pass --shallow-submodules to make submodule clones shallow as well.

Partial clone: skip file contents until needed

git clone --filter=blob:none https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

--filter asks the server to send only a subset of objects. blob:none omits all file contents (blobs) until Git needs them, for example when you check out a branch. You keep the full commit history, unlike a shallow clone. --filter=blob:limit=<size> omits only blobs at or above a size.

Clone with submodules

git clone --recurse-submodules https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

After cloning, this initialises and clones the submodules. The documentation describes it as equivalent to running git submodule update --init --recursive afterwards. It is ignored if you also pass --no-checkout, --bare or --mirror.

Name the remote something other than origin

git clone --origin upstream https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

-o (--origin) sets the name of the remote that points back to the source. This overrides the clone.defaultRemoteName setting.

Bare and mirror clones

git clone --bare https://github.com/YOUR-USERNAME/YOUR-REPOSITORY repo.git
git clone --mirror https://github.com/YOUR-USERNAME/YOUR-REPOSITORY repo.git

A bare clone has no working tree: the directory itself is the Git directory, with no .git subfolder. It is used for servers and backups, not for editing files. --mirror implies --bare and also maps all refs, not just branches, so a later git remote update in the copy overwrites them all.

Common options at a glance

OptionWhat it does
-b <name>Check out that branch or tag instead of the remote's default
--depth <n>Shallow clone with only the last n commits
--single-branchFetch history of one branch only
--filter=blob:nonePartial clone without file contents
--recurse-submodulesAlso clone submodules
-o <name>Name the remote something other than origin
--bareNo working tree
--mirrorBare copy that maps every ref
-n / --no-checkoutDo not check out anything after cloning

After you clone

Move into the new folder and look around:

cd YOUR-REPOSITORY
git status
git remote -v

The remote is named origin unless you changed it. To get new commits later, use fetch or pull; git fetch vs git pull explains the difference. Running git clone again would make a second copy, not update the first.

If the clone fails

  • Destination exists and is not empty. Pick a new folder name as the last argument, or empty the folder.
  • Permission denied with an SSH URL. Your key is missing from the account or not loaded. Use the guides linked above, or clone with the HTTPS URL instead.
  • Repository not found. Check the URL for typos. For a private repository you must be signed in with an account that has access.

Frequently asked questions

Does git clone download all branches?
By default it fetches the history of all branches and creates remote-tracking branches for them, but checks out only one: the branch the remote's HEAD points to. With --single-branch, or with --depth (which implies it), only one branch's history is fetched.
Can I clone into a folder that already exists?
Only if the folder is empty. The Git documentation states that cloning into an existing directory is allowed only if the directory is empty. Clone into a new folder name instead, or empty the folder first.
What is the difference between git clone and git pull?
git clone creates a new local copy of a repository, once. git pull updates an existing local copy with new commits from the remote. You clone to start and pull to stay current.
How do I clone a specific tag?
Use git clone --branch <tag> <url>. The documentation says --branch accepts a tag name too, in which case HEAD is detached at that commit.

Sources