2 Methods to Get All Remote Branches in Git

2 Methods to Get All Remote Branches in Git

Git branches serve as independent development paths within a project. When working with Git, especially in collaborative projects, it is essential to have visibility into all available remote branches. Listing the branches helps you track different code versions and prevents duplicate branches when working on various features or bug fixes. This tutorial provides 2 methods how to get all remote branches in Git.

Method 1 - branch argument

The most common way to list all remote branches is by using the following command:

git branch -r

Example output:

origin/2.4
origin/3.4
origin/4.x
origin/5.x
origin/HEAD -> origin/4.x
origin/master
origin/next

This command lists all remote branches prefixed with origin/, where origin represents the default remote repository.

Method 2 - ls-remote argument

Another way to retrieve all remote branches, along with their commit hashes, is by using:

git ls-remote --heads

Example output:

From https://github.com/opencv/opencv
82f8176b0634c5d744d1a45246244291d895b2d1        refs/heads/2.4
af32659937b6a23af04954a23a4a31ea520ceabc        refs/heads/3.4
7d87f3cda6837f6ae1f8346d9098c06cc081de88        refs/heads/4.x
a674ae1bcee60e12dea0c973081b8b2c776f7a68        refs/heads/5.x
7d87f3cda6837f6ae1f8346d9098c06cc081de88        refs/heads/master
a674ae1bcee60e12dea0c973081b8b2c776f7a68        refs/heads/next

This command outputs the commit hash followed by the branch reference name. It is particularly useful when you need to check the latest commit associated with each branch.

Leave a Comment

Cancel reply

Your email address will not be published.