How to Fetch All Branches in Git

John Wachira Feb 02, 2024
How to Fetch All Branches in Git

This article discusses how we can fetch all branches from a remote repository. The git fetch command is a useful utility when you want to download changes from a remote repository without necessarily updating your local branches.

Sometimes, you may have multiple remotes in one local repository and want to fetch all the branches. How do we go about this?

Fetch All Branches in Git

To fetch from all remotes, we use the git fetch command with the --all argument, as illustrated below.

$ git fetch --all

This should fetch branch changes from all remotes in your local repositories. Remember that the git fetch command does not update the local branches.

You must run the git pull command to fetch and update your local branches.

As the git fetch command, we can add the --all flag to our git pull command to pull from all changes from all remotes in our local repository, as illustrated below.

$ git pull --all

However, all your local branches must have remote-tracking branches for the command to work. To set up the remote-tracking branches for all local branches, use this one-liner:

$ git branch -r | grep -v '\->' | for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done

You can then run the git pull --all command.

In a nutshell, you can fetch from all remotes using the git fetch --all command. Keep in mind that the git fetch command does not overwrite the content of your local branches.

You will need to pull from the remotes to update your local branches, as we have discussed above.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Fetch