Git
Local Branches
Version Control
Coding
Software Development

Git, How do I list only local branches?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want to list only local branches in Git, the standard command is git branch. By default, that command shows branches that exist in your local repository only, which is why it is the right answer unless you explicitly ask Git to include remote-tracking refs.

Use git branch for Local Branches

The simplest command is:

bash
git branch

This prints local branch names and marks the current branch with an asterisk. For many day-to-day workflows, that is all you need.

text
* main
  feature/login
  bugfix/cache

This output does not include remote branches such as origin/main unless you request them with another option.

Know the Difference Between Local and Remote-Tracking Branches

A common source of confusion is that Git stores both local branches and remote-tracking branches. Local branches are your editable branch refs, such as main or feature/login. Remote-tracking branches are cached references such as origin/main that represent what Git last saw on the remote.

If you run git branch -a, you will see both kinds:

bash
git branch -a

That is useful for inspection, but it is not the command to use when the goal is local branches only.

Useful Formatting and Filtering Options

Once you know git branch is the base command, you can add formatting or filtering without losing the local-only behavior.

List branches matching a pattern:

bash
git branch --list 'feature/*'

Sort by most recently updated branch:

bash
git branch --sort=-committerdate

Show only branch names without the leading marker formatting:

bash
git branch --format='%(refname:short)'

These options are helpful when a repository has many active branches and you want a cleaner view.

Use for-each-ref for Script-Friendly Output

If you need machine-friendly output, git for-each-ref gives you more control while still targeting local branch refs.

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

refs/heads is the namespace for local branches. This is a good choice for tooling, reports, or shell scripts that need predictable formatting.

Practical Cleanup Workflow

Listing local branches often leads into cleanup. A common pattern is to review branches by recent activity, then delete merged or stale ones.

bash
git branch --sort=-committerdate
git branch --merged

That still keeps the scope local unless you ask for remote refs. Knowing that distinction helps avoid deleting or inspecting the wrong set of branch names.

Another useful detail is that git branch is optimized for people reading the terminal, while for-each-ref is optimized for tooling. Choosing the right command for the audience keeps your workflow cleaner and avoids shell parsing hacks later.

Common Pitfalls

  • Using git branch -a and then thinking Git has mixed local and remote branches unexpectedly.
  • Confusing remote-tracking refs such as origin/main with editable local branches.
  • Parsing human-oriented git branch output in scripts instead of using --format or for-each-ref.
  • Forgetting that the current branch is marked with an asterisk in default output.
  • Assuming a branch shown locally has necessarily been pushed to any remote.

Summary

  • git branch lists local branches by default.
  • Remote-tracking branches appear only when you request them with options such as -a or -r.
  • Use --list, --sort, and --format to refine local-branch output.
  • Use git for-each-ref refs/heads for script-friendly local branch listings.
  • Understanding branch namespaces prevents confusion between local and remote refs.
  • Local-branch listing is simplest when you keep human output and script output as separate practical use cases.

Course illustration
Course illustration

All Rights Reserved.