Git Reference ยท Setup and Basics
git clone command
Reference for git clone: copy a repository, choose the branch and directory, make shallow, partial or bare clones, and understand what origin and tracking mean.
git clone copies an existing repository into a new directory. It creates the directory, initializes a repository inside, adds a remote called origin that points at the source, downloads the objects, creates remote-tracking branches such as origin/main, and finally checks out one branch with a local branch that tracks its upstream.
The source can be a local path or any URL Git understands (see Git URLs). The clone is a complete, independent repository: after the clone you can work offline, and the source is only contacted again when you fetch or push.
git clone [<options>] <repository> [<directory>]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git clone <repository> git clone <repository> <directory> git clone --depth <n> --branch <name> <repository>
| Option | Description |
|---|---|
<repository> |
Path or URL of the source. |
<directory> |
Target folder. Default: the last path component of the source, without .git. It must not exist or must be empty. |
-b <name>, --branch <name> |
Check out this branch (or tag, which gives a detached HEAD) instead of the source's HEAD. |
-o <name>, --origin <name> |
Call the remote <name> instead of origin. |
--depth <n> |
Shallow clone: only the last n commits of each fetched branch. Implies --single-branch. |
--shallow-since=<date>, --shallow-exclude=<rev> |
Cut history by date or by a revision. |
--single-branch / --no-single-branch |
Fetch only one branch's history, or all branches. |
--filter=<spec> |
Partial clone: skip some objects, such as blob:none or blob:limit=1m. Missing objects are fetched on demand. |
--no-checkout, -n |
Do not check out anything after cloning. |
--bare |
Make a bare repository (no working tree). Branches are copied straight to refs/heads. |
--mirror |
Like --bare, but also copies all refs and sets up remote.origin.mirror so a later git remote update overwrites them. |
--recurse-submodules |
Also initialize and clone submodules. |
--no-tags |
Do not fetch tags. |
--sparse |
Start with a sparse working tree containing only the top-level files. |
--reference <repo>, --dissociate |
Borrow objects from a local repository to save time and space; --dissociate copies them afterwards. |
--local, --no-hardlinks |
For local paths, control whether objects are hard-linked. |
-c <key>=<value> |
Set configuration in the new repository before fetching. |
-q, -v, --progress |
Control output. |
Errors: a nonexistent source, a non-empty target, an unreachable URL or an unknown --branch stop the command and remove the half-made directory.
Usage notes
- Local paths are hard-linked. Cloning from a directory on the same disk shares object files, which is fast and safe because objects never change. Use
file://to force a real transfer, which is also what honors--depthand--filter. - Only one local branch is created, usually the default. Others appear as remote-tracking branches (
origin/feature).git switch featurecreates a tracking branch for you when the name is unambiguous. - Shallow clones save a lot of time in CI, but some commands (blame across old history, merge-base far back) are limited. Turn it into a full repository with
git fetch --unshallow. Pushing from shallow clones works on modern Git. - Partial clones keep full history but defer large blobs or trees. They need a server that allows filtering; commands that touch missing objects then trigger a fetch, so offline work can fail.
- Bare vs mirror.
--bareis for a central copy you push into;--mirroris for a backup that follows the source exactly. - Credentials. An
httpsclone asks the configured credential helper. Never put passwords in the URL: they end up in.git/configand shell history. - The first branch name is whatever the source's
HEADpoints at. If the source is empty you get a warning and an empty repository. - Cloning does not copy hooks, local config or the source's untracked files.