How to Fetch All Git Branches

While collaborating with other developers or when you use an open-source library, it is often necessary to fetch branches. Here, we demonstrate how to properly fetch remote branches.

Watch a course Git & GitHub - The Practical Guide

Fetching all branches from all remotes

To fetch all branches from all remotes, you should run the git fetch command with --all option:

git fetch --all

Updating local copies of the remote branches with the git fetch command is safe, but it does not update local branches that track the remote ones.

Updating local branches that track remotes

To update the local branches, you should pull each branch. Fetching will not create local branches that track remote one, and you have to do it manually.

To update local branches which track remote ones run the git pull command with --all option:

git pull --all

However, this will work only for your local branches which track remote ones.

To track all remote branches execute the following before git pull:

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

Fetching and Pulling

The git fetch command is designed to download commits, files, and references from a remote repository into the local one. Both git fetch and git pull are used for downloading the content from the remote repository. The git fetch command shows the progression of the central history, not forcing it to merge the changes into the repository. The fetched content does not affect the local work, and no working changes will be lost. The content should be checked out with git checkout to review commits before merging into the local repository. The git pull command downloads the new content and directly integrates it into the current working copy. This may cause merging conflicts. It is safe to use git pull only when the working copy is clean.