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.
Prerequisites
- Git installed (see how to install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
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 ofgit 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.
Note: Follow our guide to resolve merge conflicts in Git, or see the difference between git rebase and git merge.
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.
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>
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
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
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:
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
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.