![]() |
When working with Git, managing branches effectively is important for streamlined development workflows. Developers often need to view only local branches to focus on their current work without the noise from remote branches. This article will guide you through various methods to list only local branches in a Git repository. Understanding Git BranchesIn Git, branches are pointers to commits. They help manage different lines of development within a repository. Branches can be local or remote:
Knowing how to list only local branches helps in keeping your workflow focused and organized. Methods to List Only Local Branches1. Using the Git branchThe git branch command lists all local branches by default. This is the simplest way to see your local branches. Basic Command To list all local branches: git branch
This will output a list of local branches, with the current branch highlighted with an asterisk (*). Example: develop 2. Using git branch –listThe –list option with git branch provides a more flexible way to list branches, including the ability to filter them. List Local Branches To list all local branches (similar to git branch): git branch --list
List Local Branches Matching a Pattern To list local branches that match a specific pattern (e.g., all branches containing “feature”): git branch --list '*feature*'
Example: feature/login 3. Using git branch -vThe -v option with git branch lists local branches along with their most recent commit messages, providing additional context. List Local Branches with Latest Commit To list local branches with their latest commit: git branch -v
Example: develop d1e2f34 [ahead 2] Refactored user module 4. Using git for-each-refFor more customized output, git for-each-ref can be used to list local branches with specific formatting. List Local Branches with Custom Format To list local branches with their commit hashes: git for-each-ref --format='%(refname:short) %(objectname:short)' refs/heads/
Example: develop d1e2f34 List Local Branches with Commit Message To include commit messages in the output: git for-each-ref --format='%(refname:short) %(objectname:short) %(contents:subject)' refs/heads/
Example: develop d1e2f34 Refactored user module 5. Using Graphical User Interfaces (GUIs)Several Git GUIs allow you to view and manage branches more visually. These tools often provide an easy way to filter or list local branches. Examples of GUIs
Example 1: List Only Local Branches You want to see all local branches in your repository: git branch
Output: develop Example 2: List Local Branches with Commit Hashes You need to list local branches with their commit hashes for debugging: git for-each-ref --format='%(refname:short) %(objectname:short)' refs/heads/
Output: develop d1e2f34 Example 3: List Local Branches Matching a PatternYou want to list branches related to “feature”: git branch --list '*feature*'
Output: feature/login Example 4: List Local Branches with Latest Commit MessagesYou need to review the latest commit messages for all local branches: git branch -v
Output: develop d1e2f34 Refactored user module |
Reffered: https://www.geeksforgeeks.org
Git |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |