Git is a popular version control system that is used by developers to track changes made to their code. Branches are an important aspect of Git, as they allow developers to work on multiple features or bug fixes in parallel without affecting the main codebase. In Git, you can create two types of branches: local branches and remote branches.
Local branches are branches that exist only on your local machine, whereas remote branches are branches that exist on a remote repository, such as Github, BitBucket, AWS CodeCommit, or Gitlab. Remote branches are useful when you want to collaborate with other developers or when you want to work on a project from a different location. In this article, we will discuss how to list remote branches in Git.
Check Git Client
Before we begin, it is important to make sure that you have Git installed on your machine. You can check if Git is installed by running the following command in your terminal:
If Git is installed, you will see the version of Git that is installed on your machine.
List Remote Branches in Git
To list remote branches in Git, you can use the following command:
git branch -r
-r option stands for
--remote and it tells Git to list only the remote branches. When you run this command, Git will display a list of all the remote branches that exist in the remote repository.
For example, if you have a remote repository named origin and it contains three remote branches named “main”, “staging” and “development”, you will see the following output:
List Local and Remote (Both) Branches
You can also use the following command to list all the branches, both local and remote:
git branch -a
-a option stands for
--all and it tells Git to list all the branches, both local and remote. When you run this command, Git will display a list of all the branches, including the remote branches.
For example, if you have a local branch named “local1” and a remote repository named “origin” that contains three remote branches as defined in above command, you will see the following output:
In this output, the asterisk
(*) indicates the current branch, and the branches that are prefixed with remotes/origin/ are the remote branches.
In conclusion, listing remote branches in Git is a simple process that can be achieved by using the
git branch -r or
git branch -a command. These commands are useful when you want to see the remote branches that exist in a remote repository, and they can help you keep track of the changes that are made by other developers.