W3docs

How to Fetch All Git Branches

In this tutorial, you will get an answer to your problem of fetching the local and remote branches with the simplest way using Git most used commands.

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.

Fetching all branches from all remotes

To fetch all branches from all remotes, you should run the git fetch command with <kbd class="highlighted">--all</kbd> option:

fetch all branches from all remotes git

git fetch --all

Updating local copies of the remote branches with the <kbd class="highlighted">git fetch</kbd> command is safe, but it only updates remote-tracking branches (e.g., origin/main), not local branches that track them.

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 ones, and you have to do it manually.

To update local branches which track remote ones run the git pull command with <kbd class="highlighted">--all</kbd> option:

update local branches git

git pull --all

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

To automatically track all remote branches, configure Git to set up tracking by default before <kbd class="highlighted">git pull</kbd>:

to track all remote branches git

git config branch.autosetupmerge always

Fetching and Pulling

The <kbd class="highlighted">git fetch</kbd> command is designed to download commits, files, and references from a remote repository into the local one. Both <kbd class="highlighted">git fetch</kbd> and <kbd class="highlighted">git pull</kbd> are used for downloading the content from the remote repository. The <kbd class="highlighted">git fetch</kbd> 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 <kbd class="highlighted">git pull</kbd> command downloads the new content and directly integrates it into the current working copy. This may cause merging conflicts. It is safe to use <kbd class="highlighted">git pull</kbd> only when the working copy is clean.