Synopsis
git clone [--branch <name>] [--depth <n>] [--recurse-submodules] <url> [directory]
Description
The git clone command copies an existing repository (typically from a remote server) into a new local directory. It performs three actions: it creates a new directory, runs git init inside it, and then fetches all branches and history from the remote. After cloning, the remote is automatically configured as origin, and the default branch is checked out into the working tree.
Cloning supports multiple protocols: HTTPS (most common, easiest with credential helpers), SSH (preferred for write access using key-based auth), and the legacy git:// protocol. For very large repositories, partial clones, shallow clones, and sparse checkouts can dramatically reduce the amount of data transferred.
Authentication is one of the most common stumbling blocks. With HTTPS, modern Git uses credential helpers (Keychain on macOS, Credential Manager on Windows, libsecret on Linux) to cache tokens or passwords securely. With SSH, your local SSH agent supplies the key — make sure ssh-add has loaded it. After cloning, git config --get remote.origin.url confirms what was actually configured. For monorepo or supercomputer-scale repositories, partial clone (--filter=blob:none) combined with sparse checkout (git sparse-checkout init --cone) lets you work with just a slice of the project, reducing both clone time and on-disk footprint dramatically. CI pipelines benefit further from --depth=1 and --no-tags when they don't need history.
Common Options
| Option | Description |
|---|---|
--branch <name> / -b | Check out the named branch or tag instead of the default. |
--depth <n> | Create a shallow clone with truncated history. |
--single-branch | Clone only the history of a single branch. |
--recurse-submodules | Initialize and clone submodules immediately after the main clone. |
--filter=blob:none | Create a partial clone without blob content (lazy fetched on demand). |
--bare | Make a bare clone with no working tree. |
--mirror | Set up a mirror with all refs, suitable for backups. |
Examples
git clone https://github.com/user/project.git
# Standard clone over HTTPS
git clone [email protected]:user/project.git my-fork
# Clone via SSH into a specific directory
git clone --depth 1 --branch v2.0 https://github.com/user/project.git
# Shallow clone of a specific tag, useful for CI
git clone --filter=blob:none https://github.com/torvalds/linux.git
# Partial clone for huge repos — blobs fetched on demand
Common Mistakes
Cloning a giant repository without filters can take minutes or hours and consume gigabytes. For exploration or CI builds, prefer --depth 1. Another mistake is cloning over HTTPS when you intend to push changes — without credential setup, every push will prompt for a password. Use SSH or a credential helper for write access. Finally, be aware that --single-branch clones cannot easily access other branches without reconfiguring the remote.
Related Commands
git init, git fetch, git remote, git submodule