Speed up big clones with --single-branch, --depth 1 and partial clone filters, and learn how to fetch the rest of the history or other branches later.
The short answer
#Use --depth 1 to get only the latest commit, and --branch with --single-branch to get only one branch. Combined, they give you the smallest, fastest clone.
_10# only the latest commit of the default branch_10git clone --depth 1 https://github.com/example/project.git_10_10# only one branch, full history_10git clone --single-branch --branch release/2.x https://github.com/example/project.git_10_10# only the latest commit of one branch_10git clone --depth 1 --branch release/2.x https://github.com/example/project.git
Why it happens
#A normal git clone downloads every commit, every branch and every version of every file in the repository's history. For a large or long-lived project that can be gigabytes, even though you only need today's code.
Git has a few ways to download less:
- A shallow clone (
--depth) cuts the history off after a set number of commits. - A single-branch clone (
--single-branch) only fetches one branch. - A partial clone (
--filter) keeps the full commit history but downloads file contents only when you need them.
Note that --depth implies --single-branch unless you also pass --no-single-branch.
How to fix it
#Shallow clone for builds and quick looks
For CI jobs, Docker builds or reading the code, the latest commit is enough:
_10git clone --depth 1 https://github.com/example/project.git
--branch also accepts a tag, which is handy for building a specific release:
_10git clone --depth 1 --branch v3.1.0 https://github.com/example/project.git
Many CI systems already clone shallowly by default. GitHub's actions/checkout, for example, fetches a single commit unless you set fetch-depth.
Single branch with full history
If you need git log and git blame to work for one branch but do not care about the others:
_10git clone --single-branch --branch main https://github.com/example/project.git
Partial clone for day-to-day work on a big repository
A blobless clone downloads all commits and directory listings but fetches file contents on demand. History commands work, and checkouts download what they need. The server has to support it; major hosts such as GitHub and GitLab do.
_10git clone --filter=blob:none https://github.com/example/project.git
This is usually a better choice than a shallow clone if you plan to commit and push regularly.
Fetch more later
You can deepen or complete a shallow clone at any time:
_10git fetch --depth=50 # get the last 50 commits_10git fetch --unshallow # get the full history
To add another branch to a single-branch clone, tell the remote to track it and fetch:
_10git remote set-branches --add origin feature/search_10git fetch origin feature/search_10git switch feature/search
To turn it back into a normal clone that fetches every branch:
_10git remote set-branches origin '*'_10git fetch origin
Common pitfalls
#- Running
git log,git blameorgit bisectin a shallow clone and getting incomplete results. The history simply is not there. - Tools that read tags or count commits, such as
git describeor version scripts in CI, failing or giving wrong numbers in a shallow clone. Fetch more depth or the tags they need. - Being confused that
git switch other-branchfails in a single-branch clone. The branch was never fetched; add it as shown above. - Expecting
--depthto make the working tree smaller. It reduces history, not the size of the files you check out. For that, look at sparse checkout.
Related reading
#Was this resource helpful?