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
Hi! I'm Alex, creator of GitByBit.
And this is Gitopedia—a reference that summarizes knowledge in my Git course.
Gitopedia is designed to be a personal knowledge base. When you progress through the main Git course in VS Code, you unlock all of these topics, one by one, bit by bit. This gives you time to digest the information and gradually build the mental model of Git. Here's an example of how it works:
Learning directly in VS Code lets you practice Git exactly as you would in real life, doing real jobs, writing real code. In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.