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

Conteúdo do Curso

Git Essentials

Git Essentials

1. Introduction to Git
2. More Advanced Interaction
3. Undoing Changes
4. Working with Branches in Git

bookMoving Between Branches and Deleting Them

Creating and Switching to a New Branch

As we have already mentioned in the previous chapter, to switch to a particular branch, you can use the git checkout command. However, creating a branch and switching to a newly created branch is extremely common, hence why Git introduced a separate command for creating a new branch and immediately switching to it.

Basically, we should run the same git checkout command, but with the -b flag:

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:

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

Note

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

Everything will become clear with an example.

Example Workflow

Let’s first check the status of our working tree and staging area:

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

The HEAD now points to our new branch, so let’s display the two latest commits:

Our new branch and the testing/some-tests branch point to the same latest commit, and since we are now on the new branch, we can safely delete the testing/some-tests branch:

The deletion was successful, let’s now once again take a look at the two latest commits and list all branches:

The branch was indeed deleted since we only have two branches now with the new one pointing to the latest commit, and the master branch being one commit behind.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
some-alt