How to Show Remote Tracking Branches in Git

Abdul Jabbar Feb 02, 2024
  1. List Git Remote Branches
  2. Track Remote Branches in Git
  3. View Tracked Remote Branch in Git
  4. Change Branch’s Remote Tracking in Git
How to Show Remote Tracking Branches in Git

The branch on the remote Git repository is called a remote branch. These are pointers in our remote repositories, including branches, tags, etc.

There are local branches that exist only on the local personal computers for each developer, but there is only one remote branch for all the developers to merge their tasks and push them to the cloud repository.

When we clone a repository, a reference is automatically created to a remote source, referred to as remote in our local environment. It helps us check new commits and push recent changes to the remote repository.

Git repositories preserve and track the sequence of defined commits in a branch. We can find the commit automatically in the currently assigned branch, main or master, and this option is by default.

Git branches can be available through the Git command git branch. The starred (*) branch can be recognized as the currently active branch in the current repository.

List Git Remote Branches

We can list the remote branches linked with the Git repository through three different commands listed below with different parameters.

git branch -a

The git branch -a command will list all the branches on the local personal branch and all available on the remote repository.

git branch -r

The git branch -r command will list only all the branches available on the remote repository but not the local branches on the personal computer of the developers.

git remote show

The git remote show command will list only all the branches available on the remote repository with the associated metadata of the branches but not the local branches on the personal computer of the developers.

The first two commands are frequently used because they only list required branches with the details. On the other hand, the last one gives us a detailed view of each branch that is unnecessary for the developers.

Track Remote Branches in Git

We will track remote branches to develop a relationship with a local branch for various purposes. It helps us push or pull the commits from the remote to the local branch for the work we do in the local repositories.

It also helps us discover the state of the local branch, i.e., how far ahead or behind the commit are in the local branch compared to the remote branch.

View Tracked Remote Branch in Git

To view the remote tracked branch and the local branch in Git, we will use the command git branch with the flag -vv.

The result of this command will be in the format [<remote>/<branch>]. It is the list of the remotes and branches.

git branch -vv

Change Branch’s Remote Tracking in Git

Sometimes we may need to change our local branch to track some other remote branch within the same repository to set our recent branch to a prominent remote branch.

For this purpose, we will track the remote branch with the flag u with the command git branch, as shown below.

git branch -u Remote2/main
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Branch