Git

How do I List Only Local Branches in Git?

Git users often perform multiple operations on their local machines when working on extensive development projects. On the local repository, they generate multiple branches against each feature and then push them to the GitHub server. Git also allows developers to view the list of the existing local branches.

This blog will discuss the method of listing, generating, and removing local branches.

How do I List Only Local Branches in Git?

To list only the local branches in Git, the “git branch” command can be utilized. For this particular procedure, try out the following instructions.

At first, navigate to the Git root directory by typing out the provided command:

$ cd "C:\Users\nazma\Git"

To list the existing Git local branches, run the “git branch” command:

$ git branch

As you can see, all local branches are displayed. The asterisk “*” symbol before the “master” indicates the current working branch:

How to Create Local Branches Using ‘git branch’ in Git?

The “git branch” command is also used for creating a new branch in Git without switching to it. To do so, run the following command:

$ git branch gemma

Here, the “gemma” is our new branch name:

Then, execute the “git branch” command to ensure the new branch exists in the list of not:

$ git branch

According to the below-given output, the newly created branch is present in the local branch list:

How to Delete Local Branch Using “git branch” Command?

Another usage of the below-listed command is deleting local branches from the Git repository, as follows:

$ git branch -d gemma

In the above-stated command, the “-d” option indicates the “delete”, and “gemma” is the name of the branch. It can be seen that the specified branch is deleted successfully:

After that, use the provided command to verify if the branch is deleted or not:

$ git branch

The below-given output indicates that the previously deleted branch does not exist in the list:

That’s all! We have provided a way of listing, creating, and removing local branches from Git.

Conclusion

The “git branch” command can be utilized for listing only the local branches in Git. The specified command is also used for creating a new branch. However, when developers need to delete a local branch from the repository, the “git branch -d <branch-name>” command can be used. This post illustrated the process of listing, generating, and removing local branches.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.