The git branch command lets you see a list of all the branches stored in your local version of a repository. To see the remote branches associated with your repository, you need to append the -r flag to the end of the git branch command.
In this guide, we discuss how to use the git branch -r command to show remote branches. We also discuss how to use the git remote show command to show branches on the remote version of your repo.
Git: List Remote Branches Using git branch
This command returns a list of all the local repository branches:
* master dev
The asterisk (*) denotes the branch we are currently viewing. We can see that the branch that we want to create (“dev2.2-fix”) does not exist.
Before we create the branch, we want to check if the branch exists on our remote. We can do this by adding the -r flag to the git branch command:
git branch -r
This command retrieves the branches on the remote version of our repository:
origin/HEAD -> origin/master origin/dev2.2-fix origin/master
We can see that our remote repository has a branch called dev2.2-fix already. We now know that the branch exists on our remote repository but not our local one.
This means we need to fetch an existing branch to our local machine so we can get to work on writing our code. We don’t need to create a new branch.
We can fetch the existing branch from our remote repository using the Git fetch command:
git fetch origin dev2.2-fix
This will let us retrieve the dev2.2-fix branch from our origin repository. “origin” is the name of the main remote repo to which we push our code. We can see that once we run this command a new branch is created:
From https://github.com/career-karma-tutorials/ck-git * branch dev2.2-fix -> FETCH_HEAD
Git: List Remote Branches Using git remote show
The git branch -r command is sufficient if you want a brief overview of all the branches stored on the remote version of your repository.
There is a command called git remote show which displays more detailed information about all the branches on a local and remote repo. This command shows:
- All remote branches
- The local branches configured with the git pull command
- The branches configured with the git push command
For most users, this command will provide more information than they need. But, it exists if you ever need to use it.
Let’s retrieve a list of all the branches on our remote repository using the git remote show command:
git remote show origin
This will display all the remotes associated with “origin”. This is the main remote attached to our repo. Let’s take a look at what the command displays:
* remote origin Fetch URL: https://github.com/career-karma-tutorials/ck-git Push URL: https://github.com/career-karma-tutorials/ck-git HEAD branch: master Remote branches: dev2.2-fix tracked master tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (local out of date)
We can see that there are two branches on our remote repository that we are tracking. These branches are called master and dev2.2-fix. We have not configured a pull or push operation with our dev2.2-fix branch because we haven’t pulled code from or pushed code to that branch yet.
The git remote -r command lets you see a list of all the branches on a particular remote. If you need more information about the remotes associated with a repository, you can use the git remote show command.
Now you have the knowledge you need to list the remote branches in your Git repository like a pro!