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.
Git is an incredibly powerful version control system used extensively in software development for tracking changes in source code during development. While working with Git, managing branches becomes a crucial aspect because branches allow for isolated development environments. One common task while managing branches is to list only the local branches. This article will explore how you can list only local branches in Git, accompanied by technical explanations, command examples, and additional insights into branch management.
Listing Local Branches
In Git, branches are pointers to specific commits in the repository's history. Local branches exist in your local repository, while remote-tracking branches are branches from remote repositories that are tracked locally. To focus on local branches alone, you can use specific Git commands.
The most straightforward way to list all local branches is:
This command outputs the list of all local branches in the current repository, highlighting the currently checked-out branch with an asterisk (*).
Technical Explanation
When you run git branch, Git checks the current working directory for the .git directory, which contains all the metadata for the repository, including information about branches. Git then reads this directory to gather and display the list of branches that exist locally.
Command Options
The git branch command can be utilized with several options to tailor its output:
-v: This option lists local branches along with the latest commit on each branch.
--mergedand--no-merged: Use these to filter branches that are either merged or not merged into the current HEAD.
-a: Shows both local and remote branches, which can be filtered to focus only on local branches as shown below.
Filtering Only Local Branches
In some cases, you might want to ensure that only local branches are listed, without any remote tracking branches appearing. For this purpose, avoid options like -r (remote branches) and use a simple git branch to get the local ones directly.
Example Scenario
Imagine a project with multiple features and a main branch. Your branch structure is as follows:
mainfeature-1feature-2bugfix-1hotfix-1
When executing git branch, the local branches listed will be:
Each branch represents an isolated line of development. The command highlights main to indicate it's the branch currently checked out.
Additional Insights on Branch Management
Pruning Local Branches
Over time, as local branches are merged or abandoned, it's good practice to prune your branch list. This can be achieved using:
This command deletes a branch only if it has been merged with the current branch. For forceful deletion, even if it's not merged, use:
Summary Table
Below is a table summarizing key command options pertaining to local branches:
| Command | Description |
git branch | Lists all local branches. |
git branch -v | Lists local branches with the latest commit message. |
git branch --merged | Lists branches merged into the current HEAD. |
git branch --no-merged | Lists branches not merged into the current HEAD. |
git branch -d <branch-name> | Deletes the specified branch, only if it's merged. |
git branch -D <branch-name> | Forcibly deletes the specified branch, regardless of its status. |
Working with Checkouts
Switching between branches is another frequent task:
In newer Git versions, this can also be achieved with:
Both commands allow you to navigate between different branches, enabling you to work on various features or bugfixes on isolated environments.
Conclusion
Understanding how to list only local branches effectively is essential in managing your project's branching strategy. By leveraging the git branch command and its options, you maintain a clean working environment and ensure seamless navigation and pruning of branches. As your Git skills enhance, these practices become integral to securing a robust and efficient version control workflow.

