You can list the remote branches associated with a repository using the git branch -r, the git branch -a command or the git remote show command. To see local branches, use the git branch command.
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
There are three ways to list the remote branches associated with a Git repository:
- git branch -a: See both local and remote branches
- git branch -r: See only remote branches
- git remote show: See remote branches and associated metadata
The most common commands are git branch -a and git branch -r because they only list the branches. git remote show provides more detailed information about each branch which is not always necessary.
Git: List All Remote Branches Using git branch
We have a Git repository called ck-git. We’re unsure whether the branch we want to create, dev2.2-fix, exists in our repository.
The git branch -r Flag
To check for this branch, we can use the git branch command:
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
The git branch -a Flag
The -a flag associated with the git branch command returns all the local and remote branches associated with a repository.
Consider the following command:
git branch -a
Our command returns:
* master remotes/origin/activity-feed remotes/origin/master
We can see that there are branches that did not appear when we run git branch -r. This is because git branch -r only returns remote branches. git branch -a returns remote tracking branches and local branches.
Remote branches are denoted by the “remotes” label.
Git: List All Remote Branches Using git remote show
The git remote show displays detailed information about the branches associated with a remote repository. This command takes one argument: the name of the remote whose branches you want to view.
The git branch -r command is sufficient if you want a brief overview of all the branches stored on a remote. If you want more detailed information, the git remote show command may be more useful. This command returns:
- All remote branches
- The local branches configured with the git pull command
- The branches configured with the git push command
Let’s run the git remote show command on our “origin” remote, which is the name of the main remote associated with our project. We can expect to see the origin master branch, the main branch on our remote, and any other branches we have.
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 command displays 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.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
We have not configured a pull or push operation with our dev2.2-fix branch. This is because we haven’t pulled code from or pushed code to that branch yet.
Conclusion
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 use Git list branches on remote commands. To learn more about working with Git, read our How to Learn Git guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.