Git: How do I list only local branches?

Learn git: how do i list only local branches? with practical examples, diagrams, and best practices. Covers git, git-branch development techniques with visual explanations.

Git: How to List Only Local Branches

Hero image for Git: How do I list only local branches?

Learn the essential Git commands to effectively list and manage only your local branches, distinguishing them from remote-tracking branches.

When working with Git, it's common to have many branches, both local and remote-tracking. Often, you only need to see the branches that exist on your local machine, which you can directly check out and work on. This article will guide you through the various Git commands to achieve this, providing clear explanations and practical examples.

Understanding Local vs. Remote-Tracking Branches

Before diving into the commands, it's crucial to understand the distinction between local and remote-tracking branches. A local branch is a pointer to a commit in your local repository that you can directly interact with. A remote-tracking branch, on the other hand, is a local reference to the state of a branch on a remote repository. It's a snapshot of the remote branch's last known state, updated when you git fetch or git pull. You cannot directly check out a remote-tracking branch; you must create a local branch from it first.

graph TD
    A[Local Repository] --> B(Local Branch: main)
    A --> C(Local Branch: feature/new-ui)
    D[Remote Repository] --> E(Remote Branch: origin/main)
    D --> F(Remote Branch: origin/develop)
    E --> G{Remote-Tracking Branch: origin/main}
    F --> H{Remote-Tracking Branch: origin/develop}
    G -- 'Tracks' --> B
    H -- 'Tracks' --> C

Relationship between local, remote, and remote-tracking branches

The Basic Command: git branch

The simplest and most common way to list local branches is by using the git branch command without any arguments. This command will display all local branches in your repository. The currently active branch will be highlighted, usually with an asterisk (*).

git branch

Listing all local branches

The output will look something like this:

  develop
* main
  feature/login
  bugfix/typo

In this example, main is the currently checked-out branch.

Listing Branches with Additional Information

While git branch is great for a quick list, you might sometimes need more details about your local branches, such as the last commit on each branch. The -v or --verbose option can provide this.

git branch -v

Listing local branches with verbose output

This command will show the full SHA-1 hash of the last commit on each branch, along with its commit message and the upstream branch if configured:

  develop         a1b2c3d Last commit message on develop
* main            e4f5g6h [origin/main] Last commit message on main
  feature/login   i7j8k9l Implement user login functionality

Filtering Branches with grep

For more advanced filtering, especially if you want to exclude certain patterns or only show branches matching a specific naming convention, you can pipe the output of git branch to grep. This is particularly useful in larger repositories.

git branch | grep 'feature/'

Listing only local branches starting with 'feature/'

This command would only show branches like feature/new-ui or feature/login.

Excluding Remote-Tracking Branches Explicitly

While git branch by default only shows local branches, it's good to know how to explicitly exclude remote-tracking branches if you were using a command that might include them (like git branch -a). The --remotes or -r flag lists remote-tracking branches, and --all or -a lists both. To ensure you only get local, you can combine git branch with grep to filter out lines containing remotes/ or origin/.

git branch -a | grep -v 'remotes/'

Listing all branches and filtering out remote-tracking branches

The -v flag with grep inverts the match, meaning it will show lines that do not contain 'remotes/'. This effectively gives you only local branches, even if you started with git branch -a.