Git clone all branches

Greg Foster
Greg Foster
Graphite software engineer

This guide provides a detailed look at how to clone all branches from a Git repository, check out branches locally, and effectively manage your local copies.

git clone is a command used to make a copy of a target repository. By default, git clone checks out the default branch of the repository, usually main.

The command clones all of the repository’s branches by downloading their data (i.e., commits, files, and references), but it only automatically creates a local branch for the main branch. The other branches are available in the repository; you can switch to them using git checkout <branch-name>, which will create a local branch that tracks the remote branch the first time you check it out.

Let's look at how to use the git checkout command to checkout all of the branches in a repository.

Here's how you can ensure that you clone all branches and have them available locally:

Start by cloning the repository normally. This command will clone the repository and check out the default branch, which is usually main:

git clone <repository-url>

Replace <repository-url> with the actual URL of the Git repository you wish to clone.

Once the repository is cloned, you need to see all the branches available on the remote. Navigate into the cloned repository and execute the following command:

cd <repository-name>
git branch -r

This command lists all remote branches, displayed as origin/branch-name.

To work with these branches, you need to check them out as local branches. You can manually check out each branch one at a time using:

git checkout -b <branch-name> origin/<branch-name>

However, to automate the process and checkout all remote branches, you can use a loop in the command line:

for branch in `git branch -r | grep -v '\->'`; do
git branch --track "${branch#origin/}" "$branch"

This script does the following:

  • Lists all remote branches.
  • Filters out any pointers to other branches (like origin/HEAD -> origin/main).
  • Loops through each branch and creates a local branch that tracks the remote branch.

After setting up tracking for each branch, make sure your local copies are up to date:

git fetch --all

This command downloads all the data from the remote repository for all branches, ensuring that your local copies match the remote.

Now that you have all branches locally, you can switch between them using:

git checkout <branch-name>

Replace <branch-name> with the name of the branch you want to switch to.

  • Stay updated: Regularly pull changes from the remote repository to keep your branches up-to-date using git pull.
  • Clean Up: If certain branches are deleted from the remote repository, clean up obsolete tracking branches with git remote prune origin. This will delete all local references to outdated, or "stale", branches.

For more information on cloning branches into your repository, see the official Git documentation.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2