Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie, we're going to learn how to create a branch. To begin with, let's start by looking at our current branch. The way you can see all the branches in our local repository is git branch. And it's just by itself git branch will show us a list of the branches that we have on our local machine. I only have one, and it is master, the default branch. And you can see there is an asterisk next to it. That lets you know that that's the branch that we're on now. We refer to that as the current branch or the currently checked out branch. You may remember from our discussion about the HEAD pointer that the reference for the HEAD pointer is stored in the .git folder.
So cat .git/HEAD, and inside there what that file contains is a reference to one of the branches, because HEAD always points to the tip of a branch. So the HEAD says all right, here is the branch that I'm on now, and then we can go in that folder to find out what the actual tip is. I can do ls -la .git/refs/HEADs, let's just do that, let's not do master, and we'll see a directory listing of all the branches that are listed there.
When we add new branches this is where they show up, it adds a new reference in the HEADs folder. And if we take a look at what's in that, .git/refs/heads /master, you can see that that contains the SHA that points to a commit, that is the tip of the current branch --oneline, so there it is. So you can see that it points to this one right here. So now that we have a good foundation for what's going on with our existing branch, let's try creating a new branch. We do that with the git branch command again, but this time, we just provide the name of the new branch.
I'm going to call mine new feature, you can call it anything you want. There can't be any spaces in there, and you will want to stay away from punctuation. You want to keep it simple with basic letters, numbers, and underscores. So git branch new_feature, I hit Return, and it created the branch for me. How do I know that it did that? Well, git branch, there it is. I'm still on the master branch. That's still the one that's checked out, the one that I'm working on. HEAD still points to master right now. We're going to talk about switching branches in the next movie. Before we do though, let's just go and take a look at what was created in our git folder; ls -la .git/refs/heads, and there we see it.
Now there's a new reference for it in the refs/heads for new feature. What do you think that points to? .git/refs/heads/new_ feature, it points to the exact same commit right now, because I haven't made any other changes. It still points to the same commit. They now have two branches pointing at the same commit. Once we start making changes to each of those and making new commits, well then those values will change. Let's take a look at .git/HEAD, and we'll see that it points to master still, that's still the branch that we've checked out.
In the next movie, we'll talk about how we switch to the new branch.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92340 Viewers
82 Video lessons · 104105 Viewers
71 Video lessons · 75490 Viewers
56 Video lessons · 106874 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.