Programming and IT
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.
In this article
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, not another clone.
Syntax and options#
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#
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:
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:
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:
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.
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.
Step-by-step plan
- Get the repository addressOn the project page use the Code or Clone button; https is simpler, SSH needs a key.
- Pick a folderGo to the directory where the copy should appear: the clone creates a subfolder in it.
- Run git cloneWait for the Receiving objects and Resolving deltas lines — that is the end of the download.
- Go inside and checkcd into the folder, then git remote -v and git log --oneline -5: the address is saved, the history is there.
- Decide on depthOnly need the code — --depth 1; need the history — an ordinary clone.
Start learning this in your own space
The plan goes into your repository: tick off stages, keep notes — the change history shows how far you have come.
Check yourself
1.Which command puts a copy of the repository into a folder called work instead of notes?
2.What does the --depth 1 option do?
3.Under what name does Git save the address you cloned from?
Sources
-
Pro Git bookThe chapter “Git Basics” — cloning and working with remotesfree
-
git clone referenceThe full list of optionsfree
-
GitHub Docs: Cloning a repositoryWhere to find the address and how to pick https or SSHfree
Was this helpful?