How to Fetch All Git Branches

When collaborating with other developers or when you are using an open-source library, very often, you need to fetch branches using Git. This tutorial shows how to fetch local and remote git branches.

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. 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.

Do you find this helpful?

Related articles