Git List Branches: How to Show All Local & Remote Branches

git show branches

You can use Git to show all branches or use branch-related commands to list branches and their status. This helps you check recent commits, track progress, and compare work across branches. By the end of this article, you should be able to identify which branches in your project are local and which are remote.

You need to know how to use both your local repository and remote repositories to manage your commands with Git.

It’s common to create a new branch for features or bug fixes to keep your work separate before you send changes with git push.

You can push it from your local repository to the remote with git push origin branch-name when your work is ready.

Show All Branches in Git Locally

Git allows you to work with multiple branches within the same repository, where each branch represents a different line of development.

These lines of development often include features or bug fixes, which are easier to manage and track when organized into separate branches.

You can switch between them or delete them. But first, you need to list branches on your local machine using git branch command to see what’s available.

To list all branches that exist on your local machine, use this command:

git branch

This shows a simple list of branch names. It includes only the branches that your local Git repository tracks. Git adds an asterisk (*) next to the branch you have checked out.

For example:

  feature/login
* main
bugfix/session-timeout

In this case, you are on the main branch. The other two branches exist locally but are not active.

Use this command if you would like more details with remote branches included:

git branch -a

But this shows both local and remote branches. Stick with git branch command to focus only on local ones.

Another way to get the current branch name is:

git rev-parse --abbrev-ref HEAD

Both commands return the same result. The second one gives you more control if you script Git actions.

To list branch names, you can run this:

git branch -v

This shows:

  • The name of each local branch
  • The latest commit hash
  • The commit message

Here is an example of output:

* main                1a2b3c4  Fix logout issue
feature/login 5d6e7f8 Add login form
bugfix/session-timeout 9a0b1c2 Fix session timer

You can also add --verbose or --list with filters:

git branch --verbose --list "feature/*"

This shows only branches that match that pattern, along with their latest commit.

To check which remote branch a local branch tracks, use:

git branch -vv

That adds the remote tracking info:

* main                1a2b3c4 [origin/main] Fix logout issue
feature/login 5d6e7f8 [origin/feature/login] Add login form

If a local branch does not track any remote, the tracking part will be blank.

You can also use this for full branch info:

git show-branch

Or, to inspect one specific branch:

git log feature/login --oneline -n 5

That shows the last 5 commits for that branch.

In the section below, you will understand how to show only the last commit in each branch locally.

Show Last Commit on Each Branch Locally

To show the last commit on each local branch in Git, you can use a simple command. This gives you the branch name, the latest commit hash, and the first line of the commit message.

Use this command:

git for-each-ref --format="%(refname:short) %(objectname:short) %(subject)" refs/heads/

This command does three things:

  • Lists only local branches (under refs/heads/)
  • Uses refname:short to show the branch name without the full path
  • Uses objectname:short for the short commit hash
  • Uses subject to show the first line of the commit message

Example output:

main 1a2b3c4 Fix logout issue
feature/login 5d6e7f8 Add login form
bugfix/session-timeout 9a0b1c2 Fix session timer

This gives you a clean summary of each local branch and its latest work.

If you would like to see who made the last commit and when, use this version:

git for-each-ref --format="%(refname:short) %(committerdate:short) %(authorname) %(objectname:short) %(subject)" refs/heads/

This shows:

  • Branch name
  • Commit date
  • Author name
  • Short hash
  • Commit message

Example:

main 2025-05-17 Alice 1a2b3c4 Fix logout issue
feature/login 2025-05-15 Bob 5d6e7f8 Add login form

You can sort the output by the commit date:

git for-each-ref --sort=-committerdate --format="%(refname:short) %(committerdate:short) %(authorname) %(objectname:short) %(subject)" refs/heads/

That shows the most recently updated branch at the top.

You can also filter branches that are listed locally. Let’s move on to the following section to see how to do that.

Filter or Sort Local Branches of Git

Use this to show only branches that match a name:

git branch --list "feature/*"

This shows only branches that start with feature/. You can also use other patterns like:

git branch --list "*login*"

That finds any branch with “login” in the name.

To sort local branches by the time of the last commit, use:

git for-each-ref --sort=-committerdate --format="%(refname:short) %(committerdate:short)" refs/heads/

This gives you a list like this:

main 2025-05-17
feature/chat 2025-05-16
bugfix/token-expiry 2025-05-12

You can replace %(committerdate:short) with %(authorname) or %(subject) if you want more info.

Git does not support pattern filtering and sorting in one command directly with git branch, but git for-each-ref can do both.

To show only branches that match “login” and sort by date:

git for-each-ref --sort=-committerdate --format="%(refname:short) %(committerdate:short) %(subject)" refs/heads/ | grep login

This filters the output after sorting. It shows only the matching lines.

If you want to list local branches that have not been updated recently, use this:

git for-each-ref --sort=committerdate --format="%(refname:short) %(committerdate:short)" refs/heads/

That shows the oldest updated branches at the top. Let’s see how to show all remote branches in the section below.

Show Remote Branches in Git

A git remote is a connection to a version of your repository hosted elsewhere, often on a platform like GitHub or GitLab. It lets you share branches that contain features or bug fixes with your team.

Use the following command to list remote branches in Git:

git branch -r

This shows all branches that exist on remotes (like origin), but not your local copies. You will see output like:

origin/HEAD -> origin/main
origin/main
origin/feature/login
origin/bugfix/session-timeout

This list shows only the remote-tracking branches. These are references your local Git keeps to track branches from a remote repository.

Use this command to show the latest commit on each remote branch:

git for-each-ref --format="%(refname:short) %(objectname:short) %(subject)" refs/remotes/

That gives output like:

origin/main 1a2b3c4 Fix logout issue
origin/feature/login 5d6e7f8 Add login form

This helps you see what each remote branch contains, without checking them out.

If you just want to fetch the latest info, without pruning:

git fetch

You can show all local and remote branches together. Let’s see how we can do that in the following part.

Show All (Local + Remote) Branches in Git

To list all branches in Git—both local and remote—use this command:

git branch -a

This shows:

  • Local branches (without a prefix)
  • Remote-tracking branches (with a remotes/ prefix)

Example output:

  main
feature/login
bugfix/session-timeout
remotes/origin/HEAD -> origin/main
remotes/origin/main
remotes/origin/feature/login
remotes/origin/bugfix/session-timeout

Local branches appear at the top. Remote branches follow, listed under the remotes/ path. Git uses origin/HEAD to show the default remote branch.

To view just the remote ones from that list:

git branch -r

To see only local branches again:

git branch

Use this when you want to focus on the branches you have checked out or created locally.

Let’s move on to the following part to understand how to show only the last commit for the remote branch.

Show Remote Branch with Last Commit

To show each remote branch with its last commit, use this command:

git for-each-ref --format="%(refname:short) %(objectname:short) %(committerdate:short) %(subject)" refs/remotes/

This shows:

  • Remote branch name
  • Last commit hash (short)
  • Commit date
  • Commit message (first line)

Example output:

origin/main 1a2b3c4 2025-05-17 Fix logout issue
origin/feature/login 5d6e7f8 2025-05-15 Add login form
origin/bugfix/token-expiry 9a0b1c2 2025-05-12 Fix token validation

This lets you see what changed last in each remote branch without switching or pulling.

To sort from most recent to oldest:

git for-each-ref --sort=-committerdate --format="%(refname:short) %(committerdate:short) %(subject)" refs/remotes/

Example output:

origin/main 2025-05-17 Fix logout issue
origin/feature/login 2025-05-15 Add login form
origin/bugfix/token-expiry 2025-05-12 Fix token validation

This helps you find the most recently updated remote branches.

You can track the remote branch locally. Let’s move on to the next part to see how we can do that.

Track a Remote Branch Locally

This is helpful when you want to work on features or bug fixes that were started by someone else on a git remote.

You can create a local branch that links to a remote one to track a remote branch locally. This lets Git know where to fetch and push changes for that branch.

Here is the main command:

git checkout -b local-branch-name origin/remote-branch-name

This does two things:

  1. Creates a new local branch
  2. Sets it to track the given remote branch

If you want to track origin/feature/chat, run:

git checkout -b feature/chat origin/feature/chat

After that, Git will link your local feature/chat to the remote one. You can run:

git branch -vv

It will show something like:

* feature/chat  5d6e7f8 [origin/feature/chat] Add chat UI

That confirms the link.

Wrapping Up

In this article, you learned how to show local and remote branches in Git. You saw how to list branches, inspect the last commits, filter results, and track remote branches locally. You also worked with commands that show extra details like commit messages, authors, and commit dates.

Here is a quick recap:

  • Use git branch to list branches locally
  • Add -a to include remote branches
  • Use git branch -v or -vv to show commit info and tracking status
  • Run git for-each-ref for detailed, sortable output
  • Use filters and grep to narrow down results
  • Run git fetch to update remote references
  • Track a remote branch locally with git checkout -b local-name origin/remote-name

FAQs

How do I list all branches in Git?

You can list branches in a few ways:
  • git branch shows local branches
  • git branch -r shows remote branches
  • git branch -a shows both
This gives you a full picture of your project’s branches.

How can I view the last commit on each branch?

Run this to see the latest commit for each local branch:
git for-each-ref --format="%(refname:short) %(objectname:short) %(subject)" refs/heads/
To include the date and author:
git for-each-ref --format="%(refname:short) %(committerdate:short) %(authorname) %(objectname:short) %(subject)" refs/heads/
This helps you spot recent changes without switching branches.

How do I list remote branches in Git?

Use this to list remote branches:
git branch -r
To see the latest commit on each remote branch:
git for-each-ref --format="%(refname:short) %(objectname:short) %(subject)" refs/remotes/
This shows what others have pushed to the repository.

How can I track a remote branch locally?

Use this command:
git checkout -b local-branch-name origin/remote-branch-name
Example:
git checkout -b feature/chat origin/feature/chat
This creates a local branch that tracks the remote one, so you can pull and push changes more easily.

How do I filter or sort local branches in Git?

To filter branches by a name pattern:
git branch --list "feature/*"
To sort by recent activity:
git for-each-ref --sort=-committerdate --format="%(refname:short) %(committerdate:short)" refs/heads/
This helps you find active or outdated branches.

Similar Reads

Git Create Branch: Simple Steps in 50 Seconds

A Git branch is a lightweight movable pointer to one of your commits. A feature or fix is thought of…

How to Delete a Branch Locally in Git

You sometimes need to remove old or unneeded branches locally in Git. You might want to keep your project clean…

How to Delete a Remote Branch in Git

When you work with teams, you often need to remove old remote branches. To delete a remote branch in Git,…

Git Pull Rebase: How It Works, and When to Use It

Git Pull Rebase works to keep your commit history simple. Git Pull Rebase helps you reduce merge noise in your…

Git Introduction: Git and GitHub Explained Guide

If you're diving into coding, your introduction to Git and GitHub will happen in no time. Git's your personal time…

Git Delete Branch: Remove Local and Remote Git Branches

Git helps you manage code in different branches. You can delete a branch when it’s old or not being used.…

What is Version Control System? Software List Explained

One of the tools we need for software development is a Version Control System. It tracks code changes, making a…

Install Git on Windows, Ubuntu & macOS: A Quick Guide

The moment you dive into programming or development, the first thing you are going to bump into is Git. It's…

How to Switch Branches in Git: git switch vs checkout

The switch operation in Git will make it easier. Designed to simplify the process for developers, it's safer and more…

Git Pull: How to Keep Your Code in Sync

When you work on a project with friends, one writes the intro, one adds pictures, and others handle different parts.…

Previous Article

PHP mb_substr Function: How it Works with Examples

Next Article

What Is AJAX? How It Works in Web Development with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.