# git clone

git clone takes an entire repository from a server — with all its history, branches and tags — and puts a copy into a new folder on your computer. It is how you start working on any existing project, yours or someone else's.

## What happens when you clone

Cloning does three things in one go. It creates a folder, downloads the hidden
`.git` directory with the whole commit history and every branch into it, and then
lays out the files of the latest version next to it — the so-called working
directory. The copy is complete: you can browse history offline, create branches
and make commits even when the server is unreachable.

Git also remembers the address it got the data from under the name `origin`, and
that name is used by default in every command that talks to the server. You
clone a repository once; to get fresh commits you use
[git pull](https://skillok.org/topics/git-pull-command), not another clone.

## Syntax and options

```bash
git clone <url> [folder]
```

The address comes in three kinds: `https://…` (the simplest — you are asked for a
password or token on your first push), `git@server:owner/project.git` (using an
SSH key) and a plain path to a folder on disk. If you do not name the folder, Git
takes the last part of the address without `.git`.

| Option | What it is for |
| --- | --- |
| `-b <name>`, `--branch <name>` | check out the named branch instead of the default one; a tag works too |
| `--depth <N>` | download only the last N commits instead of the whole history |
| `--single-branch` | fetch one branch, not all of them |
| `--recurse-submodules` | fetch nested repositories straight away |
| `--filter=blob:none` | full history, but old file versions are fetched on demand |
| `-o <name>` | call the source something other than `origin` |
| `--bare` | a copy without working files, only the repository data |

## Example: an ordinary copy

```bash
git clone https://github.com/example/notes.git
```

```
Cloning into 'notes'...
remote: Enumerating objects: 214, done.
remote: Counting objects: 100% (214/214), done.
remote: Compressing objects: 100% (128/128), done.
remote: Total 214 (delta 71), reused 205 (delta 66)
Receiving objects: 100% (214/214), 46.21 KiB | 3.85 MiB/s, done.
Resolving deltas: 100% (71/71), done.
```

A `notes` folder has appeared. To check that the link to the server was saved:

```bash
cd notes
git remote -v
```

```
origin	https://github.com/example/notes.git (fetch)
origin	https://github.com/example/notes.git (push)
```

## Example: a fast copy without history

When you only need the current code — say, to build the project — there is no
point in pulling ten years of history:

```bash
git clone --depth 1 --branch release https://github.com/example/notes.git notes-build
```

The folder is called `notes-build` and contains a single commit of the `release`
branch. This is called a shallow clone: some history commands will not work in
it. If you do need the history later, fetch the rest:

```bash
git fetch --unshallow
```

One subtlety: if you pass a tag rather than a branch to `--branch`, Git leaves
`HEAD` detached and says so honestly. To keep working you need a branch — how to
create one is covered in the article on [git branch](https://skillok.org/topics/git-branches).

## When cloning fails

`fatal: destination path 'notes' already exists and is not an empty directory` —
a folder with that name already exists and is not empty. Either give a different
name as the last argument or remove the old folder.

`Repository not found` or `fatal: repository '…' not found` — most often a typo in
the address, but for private repositories a denied access looks exactly the same:
the server does not even confirm the existence of what it will not show you.

`Permission denied (publickey)` — your SSH key was not accepted. For a one-off
copy it is easier to use the `https` address of the same repository.

`error: RPC failed … the remote end hung up unexpectedly` — the connection broke on
a large repository. `--depth 1` followed by `git fetch --unshallow` usually helps.

## How to undo a clone

There is nothing to undo: a clone is a new folder and changes nothing else on
your system. You delete a failed or unneeded copy together with its folder
(`rm -rf notes`), and if you deleted it by mistake you simply clone again — all
the commits are on the server. The only things lost with the folder are your
uncommitted edits and branches you never pushed anywhere.

The other first-day commands are collected in the
[Git commands cheat sheet](https://skillok.org/topics/git-commands-cheat-sheet).

## Step-by-step plan

1. **Get the repository address** — On the project page use the Code or Clone button; https is simpler, SSH needs a key.
2. **Pick a folder** — Go to the directory where the copy should appear: the clone creates a subfolder in it.
3. **Run git clone** — Wait for the Receiving objects and Resolving deltas lines — that is the end of the download.
4. **Go inside and check** — cd into the folder, then git remote -v and git log --oneline -5: the address is saved, the history is there.
5. **Decide on depth** — Only need the code — --depth 1; need the history — an ordinary clone.

## Check yourself

1. Which command puts a copy of the repository into a folder called work instead of notes?
   - git clone https://example.com/notes.git work
   - git clone --dir work https://example.com/notes.git
   - git clone -o work https://example.com/notes.git
2. What does the --depth 1 option do?
   - Downloads history one commit deep instead of all of it
   - Downloads the files without the .git folder
   - Limits how deeply folders are nested when unpacking
3. Under what name does Git save the address you cloned from?
   - origin
   - master
   - source

## Sources

- [Pro Git book](https://git-scm.com/book/en/v2) — The chapter “Git Basics” — cloning and working with remotes
- [git clone reference](https://git-scm.com/docs/git-clone) — The full list of options
- [GitHub Docs: Cloning a repository](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) — Where to find the address and how to pick https or SSH

---

https://skillok.org/articles/en/git-clone-command
