Creating & Checking Out Branches

How can I create a branch? How can I make a branch active?

Transcript
Creating & Checking Out Branches

In our last video, we talked a lot about the theory behind branches. Today, we’ll look at this in practice.

Before we go and create a new branch, let’s look at the current situation. Although I haven’t explicitly created any branches in this repository, yet, there’s already a branch there. “master” is the default branch that Git creates for us with every new repository.

It’s not a magical branch - you could delete or rename it like any other branch. Although, almost all teams decide to keep this default branch in their project.

The important thing to understand is this: you are always working on a branch in Git. This is not optional.

So, let’s say we want to start working on a new feature. Since new code always involves bugs, we don’t want this to affect other topics or other people in our project. That’s why we create a new branch.

We’ll base it off our “master” branch - simply by dragging that branch and dropping it on the “branches” section header. Let’s name it “feature/signup”. Voila - that’s it.

Notice how quickly this happened. Even in large projects, this won’t take any longer. Because unlike other systems, Git doesn’t have to copy the project’s files or anything like that.

You don’t have to think long if you should create a branch - it’s cheap and easy in Git and comes with no hidden costs.

Here’s our new branch in the sidebar.

But before we start working on our new feature, we first have to make our new branch active. Right-click it and select the “Check out” option. Or simply double-click it.

And here it is: the HEAD badge is now next to our feature branch.

From this point on, all the changes and all the commits that we make only happen in this branch. No other context is affected by the changes - and the mistakes - that we might make.

Let’s make a quick, simple change to see this in practice. I just create a new, empty HTML file, add this to the staging area and commit it to the repository.

If we now look at the commit history of this branch, we see this new commit. Also, that new file is here in our working copy. No surprise so far.

Now, let’s switch back to our master branch by double-clicking it.

If we look at the commit history of that branch, we see that this last commit is not present. It happened in a different context. Also, this new HTML file is not amongst our working copy files anymore.

This is to demonstrate that these contexts are really separate from each other.

Congratulations on creating and checking out your first branch. That’s it for now - see you soon in our next video!