Git: Clone a Specific Branch
Last updated
By default git clone checks out the repository's default branch (usually main) but downloads all branches. To start on a different branch, add -b <branch>. To fetch only that branch and skip the rest - handy for big repositories or CI - also pass --single-branch.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Syntax
| Command | What it does |
|---|---|
git clone -b dev <url> | Clone and check out the dev branch |
git clone -b dev --single-branch <url> | Clone only the dev branch's history |
git clone <url> then git switch dev | Clone everything, then switch |
Which approach to use
| Goal | Command |
|---|---|
| Start on a branch, keep all branches | git clone -b dev <url> |
| Only ever need one branch | git clone -b dev --single-branch <url> |
| Already cloned, want another branch | git fetch then git switch dev |
Git clone specific branch FAQ
How do I clone a specific branch in Git?
Run
git clone -b <branch> <url> (or the long form --branch). Git clones the repository and checks out the branch you named instead of the default. All other branches are still downloaded unless you also pass --single-branch.How do I clone only one branch and nothing else?
Combine the flags:
git clone -b <branch> --single-branch <url>. This fetches only the specified branch's history, skipping every other branch - a smaller, faster clone that's useful for large repositories or CI pipelines that only need one branch.Can I switch to another branch after a single-branch clone?
Not directly, because the other branches weren't fetched. First tell Git to fetch them -
git remote set-branches origin '*' then git fetch - after which you can git switch to any branch. If you expect to need multiple branches, do a normal clone instead.What's the difference from just cloning and switching?
A plain
git clone <url> followed by git switch <branch> gives you every branch and then moves to the one you want - fine for normal use. git clone -b starts you on that branch immediately, and with --single-branch it also avoids downloading the rest.Can I practice this online?
Yes. Open the terminal playground to run
git clone in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers getting a repository step by step.