📜  git branch list - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:15:16.459000             🧑  作者: Mango

Git Branch List - Shell/Bash

Introduction

Git is a popular distributed version control system used by developers worldwide. One of the core features of Git is the ability to create and manage branches of code. Branches allow multiple developers to work on the same project simultaneously without interfering with each other's changes. Git provides a command-line interface for creating, listing, and managing branches in a repository.

In this article, we will discuss the git branch list command and how it can be used to list all branches in a repository.

Syntax

The basic syntax of the git branch list command is as follows:

$ git branch list

This command will display a list of all branches in the current repository, including the currently checked-out branch. The output will include the name of each branch and an indication of which branch is currently checked out.

Examples

To see the git branch list command in action, let's create a new repository and add some branches to it:

$ mkdir my-repo
$ cd my-repo
$ git init
Initialized empty Git repository in /path/to/my-repo/.git/
$ touch file1.txt
$ git add file1.txt
$ git commit -m "Initial commit"
[master (root-commit) b2c3966] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1.txt
$ git branch feature-branch
$ git checkout feature-branch
Switched to branch 'feature-branch'
$ touch file2.txt
$ git add file2.txt
$ git commit -m "Added file2.txt to feature-branch"
[feature-branch b3d3fa3] Added file2.txt to feature-branch
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file2.txt
$ git branch another-branch
$ git checkout another-branch
Switched to branch 'another-branch'
$ touch file3.txt
$ git add file3.txt
$ git commit -m "Added file3.txt to another-branch"
[another-branch 421daf2] Added file3.txt to another-branch
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file3.txt

Now that we have some branches in our repository, we can use the git branch list command to list them:

$ git branch list
  another-branch
  feature-branch
* master

This output indicates that we have three branches in our repository: another-branch, feature-branch, and master. The * symbol indicates that we are currently on the master branch.

Conclusion

The git branch list command is a useful tool for listing all branches in a Git repository. It can be used to keep track of multiple branches and switch between them as needed. We hope this article has been helpful in understanding the basics of listing branches in Git.