Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Moving Between Branches and Deleting Them | Working with Branches in Git
Git Essentials

bookMoving Between Branches and Deleting Them

Creating and Switching to a New Branch

To switch to an existing branch, use the git checkout command. However, since creating a new branch and switching to it immediately is a common action, Git provides a convenient option for it.

Use the same git checkout command with the -b flag:

git checkout -b <new_branch_name>

Deleting Branches

To delete a branch, use the -d flag (which stands for delete). This deletes the branch only if its changes are already merged into the current branch:

git branch -d <branch_name>

Where <branch_name> is the name of the branch you want to delete.

Note
Note

If there are unmerged changes, Git will prevent the deletion and provide a warning.

Everything will become clear with an example.

Example Workflow

First, check the status of the working tree and staging area:

Checking status

As you can see, the working tree and staging area are empty, and you are currently on the testing/some-tests branch. Now create a new branch named feature/new-feature, immediately switch to it, and then list all branches:

Creating the branch and checking it out

The HEAD now points to the new branch, so display the two most recent commits:

Two latest commits

The new branch and the testing/some-tests branch both point to the same latest commit. Since you are now on the new branch, you can safely delete the testing/some-tests branch:

Deleting the branch

The deletion was successful.
Now review the two most recent commits and list all existing branches:

Displaying two latest commits and listing branches

The branch was successfully deleted, leaving only two branches. The new branch points to the latest commit, while the master branch remains one commit behind.

question mark

Which command can be used to create a branch and immediately switch to it?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 3.57

bookMoving Between Branches and Deleting Them

Swipe to show menu

Creating and Switching to a New Branch

To switch to an existing branch, use the git checkout command. However, since creating a new branch and switching to it immediately is a common action, Git provides a convenient option for it.

Use the same git checkout command with the -b flag:

git checkout -b <new_branch_name>

Deleting Branches

To delete a branch, use the -d flag (which stands for delete). This deletes the branch only if its changes are already merged into the current branch:

git branch -d <branch_name>

Where <branch_name> is the name of the branch you want to delete.

Note
Note

If there are unmerged changes, Git will prevent the deletion and provide a warning.

Everything will become clear with an example.

Example Workflow

First, check the status of the working tree and staging area:

Checking status

As you can see, the working tree and staging area are empty, and you are currently on the testing/some-tests branch. Now create a new branch named feature/new-feature, immediately switch to it, and then list all branches:

Creating the branch and checking it out

The HEAD now points to the new branch, so display the two most recent commits:

Two latest commits

The new branch and the testing/some-tests branch both point to the same latest commit. Since you are now on the new branch, you can safely delete the testing/some-tests branch:

Deleting the branch

The deletion was successful.
Now review the two most recent commits and list all existing branches:

Displaying two latest commits and listing branches

The branch was successfully deleted, leaving only two branches. The new branch points to the latest commit, while the master branch remains one commit behind.

question mark

Which command can be used to create a branch and immediately switch to it?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 3
some-alt