git branch
git branch
: list, create, or delete branches.
The git branch
command is a versatile tool used in Git for working with branches. A branch in Git is essentially a pointer to a particular commit but represents a separate line of development in your project. Branches allow multiple developers to work on different features or fixes simultaneously without interfering with each other's work.
The primary uses of the git branch
command are to list all local branches in your repository, create new branches, or delete branches.
- Listing branches: Simply typing
git branch
will list all the local branches in your current repository. - Creating branches: To create a new branch, you use
git branch
followed by the name of the new branch you want to create. - Deleting branches: To delete a branch, you use
git branch
with the-d
flag, followed by the name of the branch you want to delete.
Examples
List all local branches in the repository:
git branch
Create a new branch named 'new-feature' (but don't switch to it):
git branch new-feature
Delete a branch named 'old-feature':
git branch -d old-feature
In recent versions of Git, the git switch
command has been introduced as a more user-friendly alternative to git checkout
for switching branches. It also allows you to create and switch to a new branch in a single command. If you're using Git 2.23 or later, you may find git switch
more intuitive for branch operations.
Switch to an existing branch named login-feature
:
git switch login-feature
Create a new branch named login-form-bugfix
and switch to it right away:
git switch -c login-form-bugfix