Moving 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.
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:
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:
The HEAD now points to the new branch, so display the two most recent 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:
The deletion was successful.
Now review the two most recent commits and list all existing 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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.57
Moving 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.
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:
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:
The HEAD now points to the new branch, so display the two most recent 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:
The deletion was successful.
Now review the two most recent commits and list all existing 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.
Thanks for your feedback!