Horje
How To List Only Local Branches?

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 Branches

In Git, branches are pointers to commits. They help manage different lines of development within a repository. Branches can be local or remote:

  • Local Branches: Branches that exist only in your local repository.
  • Remote Branches: Branches that are tracked from remote repositories.

Knowing how to list only local branches helps in keeping your workflow focused and organized.

Methods to List Only Local Branches

1. Using the Git branch

The 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
* feature/login
master

2. Using git branch –list

The –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
feature/signup

3. Using git branch -v

The -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
* feature/login c3d4f56 [behind 1] Added login tests
master e5f6a78 Initial commit

4. Using git for-each-ref

For 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
feature/login c3d4f56
master e5f6a78

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
feature/login c3d4f56 Added login tests
master e5f6a78 Initial commit

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

  • GitKraken: Lists local and remote branches in separate sections, making it easy to focus on local branches.
  • SourceTree: Provides a branch list view where you can filter to show only local branches.
  • GitHub Desktop: Allows you to view and manage local branches alongside remote branches with filtering options.

Example 1: List Only Local Branches

You want to see all local branches in your repository:

git branch

Output:

  develop
* feature/login
master

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
feature/login c3d4f56
master e5f6a78

Example 3: List Local Branches Matching a Pattern

You want to list branches related to “feature”:

git branch --list '*feature*'

Output:

  feature/login
feature/signup

Example 4: List Local Branches with Latest Commit Messages

You need to review the latest commit messages for all local branches:

git branch -v

Output:

  develop      d1e2f34 Refactored user module
* feature/login c3d4f56 Added login tests
master e5f6a78 Initial commit



Reffered: https://www.geeksforgeeks.org


Git

Related
GitHub Relative Link In Markdown File GitHub Relative Link In Markdown File
How To Retrieve Single File From A Specific Revision In Git? How To Retrieve Single File From A Specific Revision In Git?
How To Specify A Branch/Tag When Adding a Git Submodule? How To Specify A Branch/Tag When Adding a Git Submodule?
How To Skip Git Commit Hooks? How To Skip Git Commit Hooks?
Handling Git Pull Without Specifying a Warning Handling Git Pull Without Specifying a Warning

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17