Different ways to list branches in GIT [Local & Remote]


Steve Alila

GIT

 

Getting started to learn about listing branches in git

You are about to acquire an in-depth knowledge of git list branches using four specific commands. Before that, here's a quick overview of the commands:

Use following command to view all the local branches

git branch

To list all remote branches related to the current repo,

git branch -r

To list all local and remote branches

git branch -a

To list all branches with the latest commits in each branch.

git show-branch

 

To better understand git list branches, this tutorial walks you through the kernel of git branches, set up a lab to explore the (local and remote) branches, and practice the commands with relatable examples.

Let's do this!

 

Git branches explained

What is a branch in git?

A branch is an independent development line. Think of it as an application component that serves a specific purpose, irrespective of the folder you put it in.

Git, as a distributed version control system, is powered by branches called git branches. Mostly, the main branch exists, which used to be referred to as master before Github renamed it main in 2020.

The main branch serves most of the development needs. However, as more features and developers join the workflow, there arises the need for creating, renaming, deleting, and listing branches.

 

What is branch listing?

Branch listing is viewing a branch that exists in a repository.

 

When should you list branches?

The most typical scenarios to git list branches are knowing where you currently create features, confirming where to make push or pull requests, and preparing to check out a branch.

Now that you have a fundamental knowledge of git list branches, let us dive into practical usage of the knowledge.

 

Lab setup to explore git list branches

Let's create two repos and build their history to explain git list branches.

 

Create repos

I am creating a remote repo called git_list_branches on Github.

Different ways to list branches in GIT [Local & Remote]

Copy the repo's URL.

Github repo to practice git list branches

Clone and cd into it on the command line.

cd git_list_branches

Different ways to list branches in GIT [Local & Remote]

 

Build history

Before looking at practical examples of git list branches, let's build the commit history.

So far, we have one commit in history.

git log --oneline

Different ways to list branches in GIT [Local & Remote]

Create more files.

touch file1.txt file2.txt

Stage the files.

git add .

Commit them.

git commit -m "add text files"

Recheck the commit history.

git log --oneline

We have two commits in history.

Different ways to list branches in GIT [Local & Remote]

In the following sections of the tutorial, we'll apply the setup through 4 git list branches commands.

 

Four different commands to list branches in git

Example-1: git list branches using the git branch command

The most straightforward command to list branches is

git branch

It loops through all your branches in the local repo.

Currently, running the command

git branch

shows we have one main local branch.

Different ways to list branches in GIT [Local & Remote]

We did not create the branch, but Github did it for us. Besides, Github added and committed changes to our README.md file. Although we can rename the main branch, it is inadvisable because it pinpoints to the remote branch main on Github, making it easier to push changes.

Assume we need more local branches to introduce more features to the project. Let's create two local branches using

git branch feature_A

git branch feature_B

and rerun the command

git branch

to list all branches after adding two new local branches.

We have three branches, main, feature_A, and feature_B.

Different ways to list branches in GIT [Local & Remote]

The main branch gets marked with a *, meaning we reside there. In more technical terms, we have checked out the main branch. Likewise, we can git checkout any two branches to view the same decorations, as seen in the main branch. For instance,

git checkout feature_B

takes us to the independent feature_B branch with the changes made in the main branch.

We can view the changes by running the command

git log

Entering the command

git branch

marks the feature_B branch with a star, and colors it with green.

We can go ahead and switch between the branches, make commits, and list them.

 

Example-2: git list branches using git branch -r command

Let's git list branches in the remote repo.

Run the command

git branch -r

to view all the remote branches related to our current repo.

git list branches remotely

You expected to see one branch, right? Here is why we see two branches.

origin is git's default way of referencing a remote URL. So, origin/HEAD or origin/main means we push changes to the main branch, which is remotely accessible through the entry point HEAD.

HEAD is the default branch because it unites our local repo with the remote one, where we cloned the remote. Ignoring git's technicalities, we have main as the only remote branch and it is the branch we see on Github.

Different ways to list branches in GIT [Local & Remote]

Let's create more remote branches.

Check out the feature_A branch we created in example-1 above.

git checkout feature_A

Create one more file.

touch branch.html

Stage it.

git add branch.html

And commit it.

git commit -m "new remote"

Let's push the changes through feature_A.

git push --set-upstream origin feature_A

and refresh the remote, then recheck the commit history.

Different ways to list branches in GIT [Local & Remote]

To see a similar screen as mine above, click on the repo name followed <> Code. At repo's landing page, click on the drop-down next main followed by view all branches, as highlighted below.

Different ways to list branches in GIT [Local & Remote]

You can synchronize the newly created feature_A branch by clicking the green Compare & pull request button on Github. Follow Github's (green-marked) guidelines till all the changes are merged to the main branch. You should have a remote repo with two branches, like my screenshot below.

Different ways to list branches in GIT [Local & Remote]

Return to the command line and git list branches in the remote repo.

Running the command

git branch -r

shows we have added a remote branch, feature_A, in the workflow.

Different ways to list branches in GIT [Local & Remote]

Let's merge the feature_A branch, commit and push the changes to have a cleaner repo. Let's do that using the following steps:

git checkout main

git merge --squash feature_A

git commit -m "merge feature_A"

git push -f

Let's now git list branches in both local and remote repos.

 

Example-3: git list branches using git branch -a command

Doing git list branches to show both local and remote branches is simple. All you do is run the command,

git branch -a

as shown in the following image.

git list branches with -a flag

The remote branches get marked in red with two parts separated with a slash / to distinguish them from the local ones.

 

Bonus trick

Additionally, we can view all branches without marking them with green or red using the grep command . To do that, run the following command on your command line.

git branch -a | grep -v 'remotes'

Different ways to list branches in GIT [Local & Remote]

Besides the above git list branches commands, you may want to view the commit history concerning a branch. That is when the show-branch command is helpful, as explained in example-4 below.

 

Example-4: git list branches using git show-branch command

Running the command

git show-branch

lists the branches with the latest commit per branch.

Different ways to list branches in GIT [Local & Remote]

 

Conclusion

The four commands to git list branches are git branch, git branch -r, git branch -a, and git show-branch.

git branch helps you view all local branches, whereas git branch -r lists all remote branches connected to the current workflow.

Technically, combining git branch and git branch -r results in git branch -a since git branch -a reveals all local and remote branches. Better yet, use git show-branch to list branches with the latest commits in each branch.

 

Views: 42

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on LinkedIn or check his projects on GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

GoLinuxCloud Logo


We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages

JavaScript

Python

Golang

Node.js

Java

Laravel