---
title: "How do I clone a single branch or a shallow copy of a Git repository?"
description: "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."
url: https://www.danny.engineering/answer/git/how-do-i-clone-a-single-branch-or-a-shallow-copy-of-a-git-repository
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:57:00.000Z
tags: ["Git"]
---

# How do I clone a single branch or a shallow copy of a Git repository?

## 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.


```bash
# only the latest commit of the default branch
git clone --depth 1 https://github.com/example/project.git

# only one branch, full history
git clone --single-branch --branch release/2.x https://github.com/example/project.git

# only the latest commit of one branch
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:


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


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


```bash
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:


```bash
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.


```bash
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:


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


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


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


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


```bash
git remote set-branches origin '*'
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.

## Related reading

- [Git documentation: git clone](https://git-scm.com/docs/git-clone)
- [Git documentation: partial clone](https://git-scm.com/docs/partial-clone)
