The git branch command

The git branch command in Git is used to manage branches, which are like separate paths of development in your project. Each branch represents a different line of work, allowing you to make changes to your code without affecting the main project until you’re ready.

Here’s how it works:

View branches:

To see a list of existing branches and identify the active one, you use:

git branch

The branch with an asterisk (*) is the currently active branch.

Create a new branch:

If you want to start working on a new feature or fix, you create a new branch using:

git branch <branch_name>

This creates a new branch but doesn’t switch to it. You continue working on your current branch until you’re ready to switch.

Switch to a branch:

To switch to an existing branch, you use:

git checkout <branch_name>

Or, in more recent Git versions:

git switch <branch_name>

Now, any changes you make will be on the newly switched branch.

Create and switch to a new branch:

If you want to create and immediately switch to a new branch, you can use:

git checkout -b <new_branch_name>

Or, with newer Git versions:

git switch -c <new_branch_name>

This is a convenient way to start working on a new task.

In essence, the git branch command is a tool for managing different paths of development in your Git repository, making it easier to work on multiple features or fixes simultaneously.

Command options