More Git answers

by Danny
Published 2 min read

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
_10
git clone --depth 1 https://github.com/example/project.git
_10
_10
# only one branch, full history
_10
git clone --single-branch --branch release/2.x https://github.com/example/project.git
_10
_10
# only the latest commit of one branch
_10
git 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:


_10
git clone --depth 1 https://github.com/example/project.git

--branch also accepts a tag, which is handy for building a specific release:


_10
git 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:


_10
git 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.


_10
git 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:


_10
git fetch --depth=50 # get the last 50 commits
_10
git fetch --unshallow # get the full history

To add another branch to a single-branch clone, tell the remote to track it and fetch:


_10
git remote set-branches --add origin feature/search
_10
git fetch origin feature/search
_10
git switch feature/search

To turn it back into a normal clone that fetches every branch:


_10
git remote set-branches origin '*'
_10
git fetch origin

Common pitfalls

#
  • Running git log, git blame or git bisect in a shallow clone and getting incomplete results. The history simply is not there.
  • Tools that read tags or count commits, such as git describe or 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-branch fails in a single-branch clone. The branch was never fetched; add it as shown above.
  • Expecting --depth to make the working tree smaller. It reduces history, not the size of the files you check out. For that, look at sparse checkout.
#

Was this resource helpful?