Data report"State of code review 2024" is now liveRead the full report

How to clone a specific branch using `git clone`

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


How to clone a specific branch using git clone

This guide will walk you through how to clone a specific branch, explaining the necessary Git terminology and commands along the way.

Stop wrestling with Git commands

The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop Googling Git commands.

Learn more

git clone is a command used to copy a Git repository, a directory or storage space where your Git projects live, from a remote location to your local machine.

This process not only downloads the files but also the entire history of changes, allowing you to work on the project locally.

By default, git clone will clone the repository's default branch (typically named main).

However, if you want to work with a different branch, a feature branch for example, you'll need to specify that branch during the clone operation.

To clone a specific branch, use the following syntax:

Terminal
git clone -b <branch-name> --single-branch <repository-url>
  • -b <branch-name>: This tells Git which branch you want to clone.
  • --single-branch: This option limits the clone to the specified branch only, omitting all other branches from the download. It's useful for saving time and space when you're interested in just one branch.

Imagine you want to clone a branch named feature-login from a repository:

Terminal
git clone -b feature-login --single-branch https://github.com/username/project.git

This command clones the feature-login branch from the specified URL into a directory named project on your local machine.

You'll only have the feature-login branch available locally, not the entire repository's branches.

Sometimes, you might want to clone a repository and immediately switch to a different branch. While the -b option effectively does this by cloning only the specified branch, here's how you'd do it manually for educational purposes or in cases where you initially cloned the entire repository:

  1. Clone the repository (this will clone the default branch initially):
    Terminal
    git clone https://github.com/username/project.git
  2. Change directory to the cloned repository:
    Terminal
    cd project
  3. List all branches to find the one you want to switch to:
    Terminal
    git branch -a
  4. Checkout the branch you're interested in (this will create a local branch tracking the remote branch):
    Terminal
    git checkout -b feature-login origin/feature-login
The best engineers use Graphite to simplify Git

Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.

Git started for free
  • Cloning all branches: If you decide you need more than just the one branch, you can clone the entire repository (git clone <repository-url>) and then fetch all branches using git fetch --all.
  • Cloning to a new branch: If you want to clone a branch and then start a new branch from that point, first clone the branch as described above, then create a new branch with git checkout -b <new-branch-name>.

For further reading on cloning git repositories see the official Git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2