Git Clone
git clone <url> makes a complete local copy of a repository: every commit, branch and tag, plus a working folder with the default branch checked out. It is how you start working on any existing project.
bash
git clone https://github.com/ada/kettle.git # creates ./kettle
git clone git@github.com:ada/kettle.git my-kettle # SSH, into ./my-kettleWhat clone sets up
bash · try it yourself
# a project on the "server" with two branches and a tag
git init -q --bare ~/github/recipes.git
mkdir ~/author && cd ~/author && git init -q
echo "Soup" > soup.md && git add . && git commit -q -m "Add soup"
git tag v1.0
git switch -q -c desserts && echo "Cake" > cake.md && git add . && git commit -q -m "Add cake"
git remote add origin ~/github/recipes.git && git push -q --all origin && git push -q --tags origin
cd ~
git clone ~/github/recipes.git
cd recipes
ls
git branch -a
git tag
git remote -vCloning into 'recipes'...
done.
soup.md
* main
remotes/origin/HEAD -> origin/main
remotes/origin/desserts
remotes/origin/main
v1.0
origin /home/learner/github/recipes.git (fetch)
origin /home/learner/github/recipes.git (push)
Clone did several things at once:
- created the folder
recipes(named after the URL without.git), - copied all objects and created remote-tracking branches for every server branch (
origin/main,origin/desserts), - named the remote
originand setorigin/HEADto the server's default branch, - created a local
mainthat tracksorigin/mainand checked it out.
Only main is a local branch. git switch desserts creates the other one when you need it.
Clone options
| Option | Effect |
|---|---|
git clone <url> folder |
choose the folder name |
--branch desserts (-b) |
check out another branch (or a tag) instead of the default |
--depth 1 |
shallow clone: only the latest commit, much smaller for big projects |
--single-branch |
fetch only one branch |
--filter=blob:none |
partial clone: full history, file contents downloaded on demand |
--recurse-submodules |
also clone submodules |
--bare |
no working folder (for servers, or to "fork" as in the previous lesson) |
--mirror |
bare + every ref, for making an exact backup |
bash · try it yourself
git init -q --bare ~/github/recipes.git
mkdir ~/author && cd ~/author && git init -q
for dish in soup salad pasta curry; do
echo "$dish" > "$dish.md"; git add .; git commit -q -m "Add $dish"
done
git switch -q -c desserts && echo "Cake" > cake.md && git add . && git commit -q -m "Add cake"
git remote add origin ~/github/recipes.git && git push -q --all origin
cd ~
git clone -q --branch desserts ~/github/recipes.git sweet
git -C sweet branch
echo "--- shallow:"
git clone -q --depth 1 file://$HOME/github/recipes.git quick
git -C quick log --oneline
git -C quick rev-parse --is-shallow-repository
echo "--- full:"
git -C sweet log --oneline | wc -l* desserts
--- shallow:
cadaa9a Add curry
true
--- full:
5
Note:
--depthis ignored for plain local paths (Git hard-links the files instead), which is why the example uses afile://URL. Withhttps://or SSH URLs it always applies. A shallow clone can be completed later withgit fetch --unshallow.
Clone vs fork vs download ZIP
| Clone | Fork | Download ZIP | |
|---|---|---|---|
| Where | your computer | the server, under your account | your computer |
| History | full | full | none: only the files |
| Can push | if you have write access | to your fork | no repository at all |
Test yourself
After git clone, which branches exist locally (as local branches)?
- All branches of the remote
- Only the default branch; the others exist as origin/* remote-tracking branches
- None
- Only main and develop
What does git clone --depth 1 <url> give you?
- An empty repository
- A shallow clone with only the most recent commit
- Only the first commit
- A bare repository
What name does clone give the remote it cloned from?
- upstream
- origin
- main
- github