Cloning Repositories: Branches, Shallow, Partial and Sparse Clones
Everything git clone does and the options that matter: the folder name, all branches and origin/HEAD, cloning one branch, shallow clones and deepening them, partial clones with blob filters, sparse checkouts, bare and mirror clones, renaming origin, no-checkout clones, and submodules.
kvi5hnuCode Sprout team member7 min readOn this page
git clone creates a complete copy of a repository: every commit, every branch's history and every tag, plus a working tree with the default branch checked out. Most of the time git clone <url> is all you need. For very large repositories, or for scripts and CI that need only the latest files, the options in this article download much less.
What a clone does
git init -q server-repo && cd server-repo
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c lake; echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q main; cd ..
git clone server-repo trail-guide
cd trail-guide
echo "=== the files of the default branch"
ls
echo "=== branches: one local, the rest remote-tracking"
git branch -a
git branch -vv
echo "=== the remote it set up"
git remote -v
echo "=== switching to lake creates a local branch from origin/lake"
git switch lake
Cloning into 'trail-guide'...
done.
=== the files of the default branch
README.md
=== branches: one local, the rest remote-tracking
* main
remotes/origin/HEAD -> origin/main
remotes/origin/lake
remotes/origin/main
* main 7fb62a0 [origin/main] Start project
=== the remote it set up
origin /workspace/server-repo (fetch)
origin /workspace/server-repo (push)
=== switching to lake creates a local branch from origin/lake
branch 'lake' set up to track 'origin/lake'.
Switched to a new branch 'lake'
In one command, git clone:
- creates a folder named after the repository:
trail-guidefor.../trail-guide.git. Give a second argument to choose the name, or.to clone into the current, empty folder; - runs
git init, adds a remote namedoriginwith the URL, and fetches everything; - creates remote-tracking branches for every branch, such as
origin/lake, plusorigin/HEADfor the remote's default branch; - creates a local branch only for the default branch,
main, withorigin/mainas its upstream, and checks it out.
Other branches exist as remote-tracking branches. git switch lake creates a local lake from origin/lake when exactly one remote has a branch with that name, and sets its upstream. That's the "guess" Git describes when switching. git branch -a lists everything.
Choosing a branch: -b and --single-branch
git clone -b lake <url> # all branches, but check out lake
git clone -b lake --single-branch <url> # only lake's history
git clone -b v2.1 --single-branch <url> # a tag works too: detached HEAD at it
--single-branch fetches only that branch, and configures the remote to fetch only it in future. To get all branches later, restore the normal fetch rule and fetch:
git remote set-branches origin '*'
git fetch origin
Shallow clones: --depth
A shallow clone downloads only the most recent commits. --depth 1 gets just the latest commit, which is enough to build or test the current code:
git init -q server-repo && cd server-repo
for n in 1 2 3 4 5; do echo "version $n" > notes.txt; git add .; git commit -q -m "Commit $n"; done
cd ..
git clone -q --depth 2 "file://$PWD/server-repo" shallow
cd shallow
echo "=== two commits of history"
git log --oneline
git rev-parse --is-shallow-repository
git fetch -q --deepen 1
echo "=== after --deepen 1: $(git rev-list --count HEAD) commits"
git fetch -q --unshallow
echo "=== after --unshallow: $(git rev-list --count HEAD) commits"
git rev-parse --is-shallow-repository
=== two commits of history
79e3741 Commit 5
1c0803b Commit 4
true
=== after --deepen 1: 3 commits
=== after --unshallow: 5 commits
false
--depth <n>limits the history toncommits. It implies--single-branch, unless you add--no-single-branch.--shallow-since=2026-01-01keeps commits after a date, and--shallow-exclude=v1.0stops at a tag.git fetch --deepen <n>addsnmore commits,git fetch --depth <n>resets the depth, andgit fetch --unshallowdownloads the rest of the history.- Local paths ignore
--depth: Git warns "--depth is ignored in local clones; use file:// instead", so the example uses afile://URL.
Shallow clones suit CI jobs and one-off builds. For day-to-day development, they have drawbacks: git log and git blame stop at the shallow boundary, merges may fail when the merge base isn't present, and pushing from a shallow clone can be refused by some servers. A partial clone is usually the better way to save space while keeping full history.
Partial clones: --filter
A partial clone downloads all commits and trees but leaves out some file contents, fetching them on demand when a command needs them:
git clone --filter=blob:none <url> # no file contents except what's checked out
git clone --filter=blob:limit=1m <url> # skip files larger than 1 MB
git clone --filter=tree:0 <url> # also skip trees; for throwaway CI clones
With blob:none, the clone contains the full history, so git log, branching and merging all work normally. When you check out an old commit or run git diff against one, Git downloads the missing file contents from the remote then. It's the recommended way to clone a large repository for daily work. The server must support it: GitHub, GitLab and Bitbucket do.
git config --global uploadpack.allowFilter true # let the sandbox "server" accept filters
git init -q server-repo && cd server-repo
for n in 1 2 3 4 5; do echo "version $n" > notes.txt; git add .; git commit -q -m "Commit $n"; done
cd ..
git clone -q --filter=blob:none "file://$PWD/server-repo" partial
cd partial
echo "full history: $(git rev-list --count HEAD) commits"
echo "filter: $(git config get remote.origin.partialclonefilter)"
echo "file contents not downloaded yet: $(git rev-list --objects --all --missing=print | grep -c '^?')"
git show HEAD~4:notes.txt # the oldest version: fetched on demand
echo "still missing: $(git rev-list --objects --all --missing=print | grep -c '^?')"
full history: 5 commits
filter: blob:none
file contents not downloaded yet: 4
version 1
still missing: 3
Four old versions of notes.txt were left out. Showing the oldest one downloaded it from the remote automatically, leaving three missing. Article 44 covers large repositories in depth.
Sparse checkout: only some folders
A sparse checkout fills the working tree with only the folders you choose, while the repository still holds everything. It pairs well with a partial clone for large monorepos:
git clone --filter=blob:none --sparse <url>
cd repo
git sparse-checkout set docs services/maps # check out these folders (plus top-level files)
git sparse-checkout add tools # add another
git sparse-checkout disable # back to everything
--sparse starts with only the files at the top level. Article 44 explains cone mode and the rest.
Bare and mirror clones
git clone --bare <url>creates a repository with no working tree, the form servers use. Its folder is conventionally namedname.git.git clone --mirror <url>is a bare clone that copies every reference exactly, including the remote's own remote-tracking branches, notes and pull-request references, andgit remote update --prune(orfetch.prune=true) keeps it identical, including deleting references that the source deleted. It's used for backups and for moving a repository to another server:git clone --mirror old-url, thengit push --mirror new-url.
Other useful options
| Option | Effect |
|---|---|
-o upstream | name the remote upstream instead of origin |
--no-checkout (-n) | clone without creating files in the working tree. git switch <branch> fills it in later. Useful before setting up a sparse checkout |
--recurse-submodules | also clone the submodules the project uses (article 41). --recursive is the older spelling |
--no-tags | don't fetch tags |
--reference <path> | borrow objects from a local repository you already have, to save time and space |
-c key=value | set a configuration value in the new repository, such as -c core.autocrlf=false |
--bundle-uri=<uri> | first download a prepared bundle file, then fetch only what's newer. Speeds up very large clones when the server offers bundles |
Cloning over HTTPS and SSH
git clone https://github.com/acme/trail-guide.git # asks for a username and token if private
git clone git@github.com:acme/trail-guide.git # uses your SSH key
The URL you clone with becomes origin's URL, so choose the protocol you want to keep using, or change it later with git remote set-url (article 17). Article 21 covers authentication and proxies.
What's current
- Partial clone with
--filter=blob:noneis the modern answer to large repositories, keeping full history while saving space. Many teams now prefer it to shallow clones for development. git sparse-checkoutwith cone mode, and Scalar, which sets up partial clone, sparse checkout and background maintenance in one command, are covered in article 44.--bundle-urilets servers and CDNs speed up initial clones with pre-built bundles.- CI systems such as GitHub Actions use shallow clones by default (a depth of 1). Set the depth to 0 for full history when a job needs tags or
git describe.
Common questions
Why do I only see main after cloning? Where are the other branches?
They're there as remote-tracking branches: git branch -a lists them. git switch <name> creates a local branch from one.
Can I clone only one folder of a repository?
Not the history of a single folder, but a sparse checkout limits the working tree to that folder: git clone --filter=blob:none --sparse <url>, then git sparse-checkout set <folder>. To download just the files, without Git, hosting services offer archive downloads, and git archive --remote works where the server allows it.
What's the difference between a shallow and a partial clone? A shallow clone cuts off history, so older commits aren't there at all. A partial clone has every commit but downloads some file contents only when needed. For development, partial clones behave like full clones.
How do I turn a shallow clone into a full one?
git fetch --unshallow. If it was also single-branch, run git remote set-branches origin '*' and git fetch to get the other branches.
Is cloning the same as downloading a ZIP? No. A ZIP holds only the files of one commit, with no history and no connection to the remote. A clone is a full repository you can commit to, update and push from.
Summary
git clone <url> [folder]copies a repository, sets uporigin, creates remote-tracking branches for every branch, and checks out the default branch.git switch <branch>creates a local branch fromorigin/<branch>.-b <branch>chooses what to check out, and--single-branchfetches only that branch.--depth <n>makes a shallow clone, which--deepenand--unshallowextend. Usefile://for local paths.--filter=blob:nonemakes a partial clone with full history and on-demand file contents.--sparsewithgit sparse-checkoutlimits the working tree.--bareand--mirrormake repositories without working trees, for servers, backups and migrations.
Practice questions
A CI job only needs to build the latest commit of main from a huge repository. Which clone command downloads the least?
Answer
git clone --depth 1 --single-branch -b main <url>
--depth 1 already implies --single-branch. Adding --filter=blob:none doesn't help much here, because the checkout needs the latest file contents anyway, but --filter=tree:0 with a shallow clone can trim more for very large trees.
After git clone <url>, git branch shows only main. A teammate says the branch lake exists. What do you run to start working on it?
Answer
git switch lake. Git finds origin/lake, creates a local lake from it, sets its upstream to origin/lake, and switches to it. If the branch was created after your clone, run git fetch first.
You need to move a repository, with all branches and tags, from one hosting service to another. Which commands?
Answer
git clone --mirror https://old.example.com/acme/trail-guide.git
cd trail-guide.git
git push --mirror https://new.example.com/acme/trail-guide.git
--mirror copies every reference exactly, and push --mirror makes the new repository's references match. Pull requests, issues and other hosting features have to be migrated with the services' own tools.
Previous: Remote Repositories: add, rename, remove, set-url, show and ls-remote
Next: Fetching and Pulling: Remote-Tracking Branches, Merge or Rebase, and Divergent Branches

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.