How can I show all the branches in a repository?

Learn how can i show all the branches in a repository? with practical examples, diagrams, and best practices. Covers git development techniques with visual explanations.

Mastering Git: How to View All Branches in Your Repository

Hero image for How can I show all the branches in a repository?

Learn the essential Git commands to list local and remote branches, understand their relationships, and effectively manage your repository's branching structure.

Git is a powerful version control system that allows developers to manage changes to their codebase efficiently. A core feature of Git is branching, which enables parallel development efforts without interfering with the main codebase. As projects grow, so does the number of branches, making it crucial to know how to view and manage them. This article will guide you through the various Git commands to display all branches, both local and remote, helping you maintain a clear overview of your repository's development landscape.

Understanding Local and Remote Branches

Before diving into the commands, it's important to distinguish between local and remote branches. Local branches exist only on your machine, representing your current work. Remote branches, on the other hand, are references to the state of branches on a remote repository (like GitHub, GitLab, or Bitbucket). When you fetch or pull changes, Git updates your local copies of these remote branches, but they are distinct from your own local development branches.

graph TD
    A[Local Repository] --> B("git fetch");
    B --> C[Remote Tracking Branches (e.g., origin/main)];
    C --> D[Local Branches (e.g., main, feature/x)];
    D --> E("git push");
    E --> F[Remote Repository];
    F --> C;

Relationship between Local and Remote Branches in Git

Listing All Local Branches

The most fundamental command to see your local branches is git branch. This command, when run without any arguments, lists all the local branches in your repository. The currently active branch will be highlighted, usually with an asterisk (*) next to its name.

git branch

Basic command to list local branches

The output will typically look something like this:

  develop
* main
  feature/new-feature

Here, main is the currently checked-out branch.

Listing All Remote Branches

To see branches that exist on your remote repository, you need to use a different flag with the git branch command. The -r (or --remotes) flag will show you all remote-tracking branches. These are local copies of the branches on your remote repository. They are read-only and serve as a reference to what's on the remote.

git branch -r

Command to list remote-tracking branches

The output might look like this:

  origin/HEAD -> origin/main
  origin/develop
  origin/main
  origin/feature/another-feature

origin is the default name for your remote repository. origin/HEAD -> origin/main indicates that origin/main is the default branch on the remote.

Listing All Branches (Local and Remote)

Often, you'll want to see a comprehensive list of all branches, both local and remote, in one go. The -a (or --all) flag combines the output of git branch and git branch -r, giving you a complete picture of your repository's branching structure.

git branch -a

Command to list all local and remote branches

This command will produce an output similar to:

  develop
* main
  feature/new-feature
  remotes/origin/HEAD -> origin/main
  remotes/origin/develop
  remotes/origin/main
  remotes/origin/feature/another-feature

This unified view is incredibly useful for understanding the full scope of development within your project.

Pruning Stale Remote Branches

Over time, remote branches that have been deleted on the remote server might still appear in your local git branch -r or git branch -a output. This is because git fetch only updates existing remote-tracking branches and adds new ones, but doesn't remove those that no longer exist on the remote. To clean up these stale references, you can use the git remote prune command or the --prune flag with git fetch.

git remote prune origin
# OR
git fetch --prune origin

Commands to remove stale remote-tracking branches