Git List Branches: How to Show Local, Remote & All Branches (Examples)

Git List Branches: How to Show Local, Remote & All Branches (Examples)

Git list branches commands help you view local, remote, and all branches in a repository as part of working with git branch basics. Using git branch, git branch -r, and git branch -a, you can quickly check available branches and manage your workflow efficiently as part of a complete Git workflow.


Git list branches - quick cheat sheet

TaskCommand
List local branchesgit branch
List remote branchesgit branch -r
List all branches (local + remote)git branch -a
List branches with last commitgit branch -v
List all branches with tracking infogit branch -vv
List merged branchesgit branch --merged
List unmerged branchesgit branch --no-merged
List remote branches (all, even unfetched)git ls-remote --heads origin
List all references (advanced)git for-each-ref --format='%(refname:short)' refs/heads/
List remote repositoriesgit remote
List remote repositories with URLgit remote -v
Show detailed remote infogit remote show origin
Fetch latest remote branchesgit fetch
Fetch all remotesgit fetch --all
Remove stale remote branchesgit remote prune origin
Search branch by namegit branch | grep <name>
Show branches sorted by last commitgit branch --sort=-committerdate
List only local branches (explicit)git for-each-ref refs/heads/
List only remote tracking branchesgit for-each-ref refs/remotes/

Git list branches

Understanding the difference between local and remote branches is the foundation of working efficiently with Git. Local branches exist in your system, while remote branches are hosted on repositories like GitHub or GitLab and are managed using git remote.

List only local branches using git branch

To view all branches available in your local repository, use:

text
git branch

This command lists only local branches and highlights the current branch with an asterisk (*).

List only remote branches using git branch -r

To see branches available on the remote repository (tracked locally), use:

text
git branch -r

This shows remote-tracking branches such as:

text
origin/main
origin/dev

List all branches using git branch -a

To display both local and remote branches together:

text
git branch -a

This is useful when you want a complete view of all available branches.

Understand origin/HEAD references

You may see entries like:

text
origin/HEAD -> origin/main
  • origin → default remote repository name
  • HEAD → pointer to the default branch on remote
  • origin/main → actual default branch

This helps Git identify which branch is used by default when cloning or pulling.


List local branches in Git

Check current branch (* symbol explained)

Run:

text
git branch

Example output:

text
* main
    feature-login
    bugfix-issue1

The * symbol indicates the current active branch where you are working.

Create and verify branches

Create new branches:

text
git branch feature_A
git branch feature_B

Verify branches:

text
git branch

Now you will see:

text
* main
  feature_A
  feature_B

This confirms that the new branches have been successfully created and are available for use with git branch basics.


List Remote Branches in Git

Sometimes remote branches exist but are not visible locally. This usually happens because your local repository is not updated with the latest remote state, which can be resolved using git fetch.

Why git branch -r may not show all branches

The command:

text
git branch -r

only shows branches that are already fetched and tracked locally. Newly created branches on remote will not appear until you fetch them.

Use git fetch to sync remote branches

To update your local repository with the latest remote branches:

text
git fetch

After fetching, run:

text
git branch -r

to see updated remote branches.

Use git ls-remote to see all remote branches

To view all branches directly from the remote without fetching:

text
git ls-remote --heads origin

This command queries the remote repository and lists all available branches without needing git clone.

Difference between fetched vs unfetched branches

  • Fetched branches → Available locally after running git fetch
  • Unfetched branches → Exist on remote but not yet visible locally

To always stay updated, regularly run:

text
git fetch --all

Check and Verify Remote Repositories

List all remotes using git remote -v

To view configured remote repositories along with their URLs using git remote:

text
git remote -v

Example output:

text
origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

Show remote details using git remote show origin

To get detailed information about a specific remote:

text
git remote show origin

This shows:

  • tracked branches
  • default branch
  • fetch/push configuration

Identify upstream branch configuration

To check which local branch is linked to which remote branch:

text
git branch -vv

Example:

text
main  abc123 [origin/main] latest commit message

This indicates the local branch is tracking origin/main.

Check which branch tracks which remote

To explicitly set or verify tracking:

text
git branch --set-upstream-to=origin/main

Tracking branches allow simplified commands like git pull and git push.

text
git pull
git push

without specifying remote and branch every time.


Filter, Search and Format Branch Output

When working with repositories that have many branches, filtering and formatting output becomes essential to quickly find relevant information.

Search branch by name using grep

To search for a specific branch:

text
git branch | grep <branch-name>

Example:

text
git branch | grep feature

This helps locate branches matching a pattern before switching using git checkout.

Sort branches by last commit date

To list branches sorted by most recent activity:

text
git branch --sort=-committerdate

This is useful to identify recently updated branches before merging changes using git merge.

Show only active branches

To view only branches that are actively being worked on (recent commits):

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

This provides a cleaner and more customizable output when managing repositories with multiple branches using git rebase.

Format output using git for-each-ref

For advanced formatting:

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

You can customize output to include:

  • branch name
  • commit date
  • author
  • tracking info

List Branches with Additional Details

Sometimes you need more than just branch names — such as commit details, merge status, or tracking info.

Show last commit on each branch

To display branches with their latest commit:

text
git branch -v

This shows:

  • branch name
  • last commit hash
  • commit message

Show merged vs unmerged branches

To list branches already merged into the current branch:

text
git branch --merged

To list branches not yet merged:

text
git branch --no-merged

This helps in cleanup and review processes before removing branches using git delete branch.

Identify stale or unused branches

To find branches not updated recently:

text
git branch --sort=committerdate

Older branches appear at the top, helping identify stale branches before cleanup using git prune.

Display tracking information

To see which remote branch each local branch tracks:

text
git branch -vv

Example output:

text
feature1  abc123 [origin/feature1] commit message

Troubleshoot Missing Remote Branches

Branch exists on GitHub but not locally

If a branch is visible on GitHub but not in your local repo, it means your local repository is not synced and needs git fetch.

Fix using git fetch --all

Fetch all updates from all remotes:

text
git fetch --all

Then verify:

text
git branch -r

Clean stale references using git remote prune

To remove references to deleted remote branches:

text
git remote prune origin

This ensures your branch list is clean and accurate using git prune.

Handle deleted remote branches

Sometimes remote branches are deleted but still appear locally. To clean them:

text
git fetch --prune

or

text
git remote prune origin

Frequently Asked Questions

1. How to list all branches in Git?

Use git branch -a to list all local and remote branches in a Git repository.

2. How to list local branches in Git?

Use git branch to show all local branches in your repository.

3. How to list remote branches in Git?

Use git branch -r to list all remote branches connected to your repository.

4. What is git show-branch?

git show-branch displays branches along with their latest commits for comparison.

5. How to check current branch in Git?

Use git branch to see the current branch marked with an asterisk (*).

6. Why git branch -r not showing all branches?

git branch -r only shows fetched branches. Run git fetch or git fetch --all to update remote branch list.

7. How to list remote repositories in Git?

Use git remote -v to list all configured remotes along with their URLs.

8. What is git ls-remote used for?

git ls-remote lists references from a remote repository without fetching them locally.

9. How to remove stale remote branches in Git?

Use git remote prune origin or git fetch --prune to clean up deleted remote branch references.

Summary:

To effectively manage branches in Git:

  • Use git branch, -r, and -a for quick listing
  • Use git fetch to stay updated with remote changes
  • Use git branch -vv for tracking details
  • Use filtering and sorting to manage large repositories effectively along with git branch basics.
  • Use prune commands to clean stale branches

Choosing the right command based on your use case improves productivity and avoids confusion.


Official Documentation

Steve Alila

Steve Alila

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.