How to Pull All Branches in Git

March 16, 2023

Introduction

Git is a version control system that allows users to maintain multiple development lines, i.e., branches, in a single project. When you start working on a project and want to clone the repository to a local machine, Git lets you retrieve individual branches or all remote branches at once.

In this tutorial, you will learn how to pull all branches in Git.

How to pull all branches in Git - a tutorial.

Prerequisites

Pulling All Branches in Git

Git branches can be stored in a remote or local repository. When you want to work on a feature from a branch stored in a remote repository, you must download it to the local repository first.

The two Git commands used to download content from a remote repository are git pull and git fetch:

  • git fetch is the safer version of git pull. It downloads the remote content without updating the local repository's working state, which means your work remains intact.
  • git pull is more aggressive because it downloads the content from the remote repository and executes git merge on the local active branch. The merge creates a new merge commit and integrates the content with your work. However, merging content like that can cause conflicts with work in progress, and it may require manual resolution.

The sections below show how to pull all Git branches to a local repository using the two commands.

Git Fetch Method

With git fetch, you can download metadata from the remote repository without affecting your local work. It is a useful option when you want to check if another developer has made any changes in the remote repository. git fetch is also used when cloning a repository and downloading the data to a local machine.

The git fetch command retrieves the latest updates from the remote repository. Depending on whether you want to download metadata for individual branches or all branches, the command has the following variations:

git fetch <remote> <branch>
  • The command fetches only the specified branch from the remote repository.
git fetch --all
  • A command is considered a power move since it fetches all the registered remotes and their branches.

For this tutorial, we will clone a new repository and fetch all the associated branches. Follow the steps below:

1. Open a Git bash command prompt on Windows or open a new terminal window in Linux (Ctrl+Alt+T) or macOS.

2. Navigate to the directory where you want to store the repository files. Use the cd command to change the directory.

3. On GitHub, locate the remote repository you want to clone and click the <> Code button. For example, we will clone phoenixNAP's Bare Metal Cloud API software development kit repository. The BMC GitHub Actions platform allows you to automate tasks in your Git repository.

4. From the dropdown menu, select the security option you want to use for Git and copy the URL. In this tutorial, we will use HTTPS since it is the easiest to set up. If you want a more secure option, go with SSH.

Copy the URL for cloning a Git repository.

5. In Git bash, clone the remote repository using the git clone command and the URL that you copied. The syntax is:

git clone <URL>
Clone a Git repository.

6. After cloning the repository, check the branches available in the local and remote repositories. To check the local branches, run:

git branch

Check remotely available branches by running:

git branch -r
List local and remote branches in Git.

Only the develop branch is available in the local repository, which means we need to fetch the remaining ones.

7. Fetch the metadata for remote branches and start tracking them. Run:

git fetch --all

The --all flag tells Git to fetch the metadata for all the branches in the repository.

8. After fetching the metadata, start tracking the branches with this command:

git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
Start tracking remote branches in Git.

The command sets up the branches in the local repository and makes them track their counterparts in the remote repository.

Run git branch again and check if the branches exist locally:

List all local branches in a Git repository.

In the example above, we see that all the branches from the remote repository now exist in the local one as well.

Note: Read our comparison of SSH and HTTPS for Git.

Git Pull Method

The git pull method is a combination of git fetch and git merge. The command retrieves branch metadata from the remote repository and updates your local working copy to reflect the changes.

Note: The git pull method works best in an existing repository where you want the changes implemented by other developers to reflect in your local repository. Cloning a repository and then running git pull would yield no result, as everything is already up to date.

Follow the steps below to pull all remote branches:

1. Open a Git bash window and change the location to your local repository.

2. Run the following command to ensure Git starts tracking all the remote branches, including the ones that don't exist in your local copy:

git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

7. Pull all branches from the remote Git repository:

git pull --all
Pull all branches in Git.

The output shows that all the remote branches have been successfully pulled and displays the changes.

Note: Resolve possible conflicts before introducing the changes by comparing Git branches.

Conclusion

This tutorial showed how to pull all branches from a remote repository to your local working copy. There are two methods available - git fetch and git pull, each suitable for its own use case.

Learn more about Git in our Git beginner's guide, or download a handy Git commands cheat sheet and save it for future use.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Git Tag: An Overview of the Basic Functions
September 6, 2022

This guide shows an overview of basic functions to perform with Git tags. See how to create, delete, push, replace, or checkout a Git tag.
Read more
How to Restore a Git Repository
August 1, 2022

Deleted or overwritten a repository? No backup? Don't worry just yet, this guide covers some steps to try and restore the repository.
Read more
How to Use Git Stash
September 13, 2022

Git stashes store unfinished work locally, without having to commit the changes and synchronize them with a remote repository. See how to create and use Git stash.
Read more
How to Remove Untracked Git Files and Folders
March 8, 2023

Untracked files and folders in a Git repository can often clutter the project. Learn to remove untracked files and folders in Git.
Read more