TExcelTAD
Home › Git › Git Clone

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-kettle

What 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 -v
Cloning 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:

  1. created the folder recipes (named after the URL without .git),
  2. copied all objects and created remote-tracking branches for every server branch (origin/main, origin/desserts),
  3. named the remote origin and set origin/HEAD to the server's default branch,
  4. created a local main that tracks origin/main and 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: --depth is ignored for plain local paths (Git hard-links the files instead), which is why the example uses a file:// URL. With https:// or SSH URLs it always applies. A shallow clone can be completed later with git 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)?

  1. All branches of the remote
  2. Only the default branch; the others exist as origin/* remote-tracking branches
  3. None
  4. Only main and develop

What does git clone --depth 1 <url> give you?

  1. An empty repository
  2. A shallow clone with only the most recent commit
  3. Only the first commit
  4. A bare repository

What name does clone give the remote it cloned from?

  1. upstream
  2. origin
  3. main
  4. github