Git offers several commands for listing branches that display the names of all the branches created in a project (Git List Branches). You can utilize the following commands to display the branches in either the local or remote repository. Let’s explore how they work.
Table Of Contents
In software development, a git branch refers to a new path of development that diverges from the main codebase, allowing for separate changes to be made without affecting the main code.
This article will guide you on how to create a new branch in Git.
The purpose of this article is to demonstrate a set of commands that can be used to list all branches in git, as well as to identify the currently active git branch.
How to List All Branches in Git for the Local Repository
To list all branches in Git for the local repository, you can use the following command:
This will display a list of all branches in the repository, with an asterisk (*) next to the currently active branch. If you have a large number of branches, you may need to scroll through the list to find the branch you’re looking for.
If you look at the above image you will see 4 branches, which are as the following list:
Alternatively, you can use the following command to display a more compact list of branches:
git branch --list
This will display a list of branch names, without any additional information.
To view all branches both remotely and locally, you need to include the “-a” flag, which will display a list of all branches within the project.
git branch -a
After executing this command, the output will be comparable to the image below.
However, if you’re wondering how to display the names of the remote branches, don’t worry, I will cover that in the next section. So, let’s continue.
Show Git Branches Remotely
To display all remote branches, you can use the same command, but include the “-r” flag, which stands for “–remotes”.
git branch -r
Alternatively, you can use the following command to display all remote branches:
git branch --remotes
Executing either of these two commands will produce the same result as shown in the image below.
Assuming that you have cloned the repository to another location and created a new branch called “cars” with some changes, the following command can be used to navigate back to the old git folder and demonstrate how to view remote branches without downloading them:
git ls-remote --heads origin
Or use the same command with only ls-remote.
The result of two both would be like the below image.
Here, “refs/heads/master” and “refs/heads/cars” are the branches on the remote repository.
Using Git to List All Branches Locally and Remotely
To view detailed information for a specific branch, either locally or remotely, you can use the following command with the “-vv” flag:
For local branches:
git branch -vv
This will display a list of all local branches with additional information, such as the last commit message, the author, and the timestamp.
For remote branches:
git branch -r -vva
This will display a list of all remote branches with additional information. Such as the last commit message, the author, and the timestamp.
To view detailed information for both local and remote branches, you can use the following command with the “-vva” flag:
git branch -vva
However, this will display a list of all local and remote branches with additional information. Such as the last commit message, the author, and the timestamp.
In conclusion, Git’s ability to list branches is an important feature for any developer working with version control.
The ability to view both local and remote branches, as well as detailed information about each branch, can help streamline the development process and keep projects organized. By using the simple commands discussed in this article.
Also, developers can quickly and easily list all branches in a Git repository, view the information they need, and take appropriate action to resolve conflicts and maintain project stability.
Overall, understanding how to list branches in Git is a fundamental skill that can benefit any developer working with version control.
To find out more, kindly navigate to the official page.