From the course: Complete Guide to Git

Create branches

- [Instructor] In this movie, we will learn how to create branches in Git. I'm inside the root of my project directory, and we're going to use the first branch command, which is just simply git branch. The branch command will give us a list of all of the branches that are in our local repository. At the moment, we only have one branch, and that's our main branch. Notice that there's an asterisk to the left of the name, and that the name is colored green. These are indications that the current branch is main. It's the branch that the head pointer is referencing. These are the versions of the files that are in our working directory. To make a new branch, we just use the git branch command again, but this time we tell it the name of a branch. So let's call our first branch new_feature. Branch name should not have any spaces or punctuation in them. Just letters, numbers, and underscores. I'll hit Return and it doesn't give me any feedback. But if I type git branch, you can see now there are two branches. And it's clear the branch that I'm on right now is still the main branch, but there's also this second branch called new_feature. We'll talk about switching branches in the next movie. For now, just recognize that we've created this new branch. It's that easy. We now have a new branch. If we take a look at the head file, we saw how to do that before, inside that git head directory, you can see it's still points to our main branch. That is still the current branch. But if we take a look at the refs heads directory, you'll see that now there's two entries there. There's one called main and one called new_feature. And if we look at what's inside each one of those, refs heads main, you can see it points to the first commit, the most recent commit. And if we change that to new feature, you'll see that it also points to that same commit. So there's two branches, but the tip of each of those branches right now is the same commit. And if we type git log --oneline, I'll clear my screen so you can see it, you can see it's that same SHA, and it tells me that the head is pointing to main But both main and new_feature are references to this same commit. So right now, I could refer to this commit by any of these names, the SHA, head, main, or new_feature, and it would all be referencing the same thing. In the next movie, we'll learn how to switch to that new_feature branch.

Contents