How can I fetch all Git branches from a remote repository and list them locally? When I clone a Git repository, I only see the master branch with git branch
, but I want to see all branches like master
, staging
, etc...
.
To fetch all Git branches from a remote repository and list them locally, you can use the following commands:
git fetch --all
git branch -a
The git fetch --all command fetches all branches from the remote repository, and git branch -a lists both local and remote branches.
Another approach is to use the git clone command with the --mirror option to clone the remote repository along with all its branches:
git clone --mirror <remote_repository_url>
After cloning with --mirror, you can use git branch -a to list all branches.
If you’ve already cloned the repository and want to fetch all branches later, you can use the following commands:
git remote update origin --prune
git branch -a
The git remote update origin --prune command fetches all branches from the remote repository and prunes any remote-tracking branches that no longer exist on the remote. The git branch -a command then lists all branches, including the newly fetched ones.