4.3 Creating Branches
Branching is one of Git’s best features, and it’s something you should use liberally. We’ll talk about creating branches and switching between them in this video; of course, we’ll also make some commits on those other branches.
1.Introduction3 lessons, 19:10
2.Basic Commands4 lessons, 23:13
3.A Bit of Theory2 lessons, 23:37
4.Logs and Branches4 lessons, 33:30
5.Github and Remotes4 lessons, 41:49
6.Intermediate Usage6 lessons, 41:55
7.Conclusion1 lesson, 01:07
4.3 Creating Branches
We've talked about branches in theory already, but now its time to actually start using them. I don't really think there's a better name for what branch actually does than branch itself. Heres the idea. You wanna experiment with something on your project, or maybe you wanna fix a bug, but you don't want to accidentally break your existing code. So what we'll do is we create a branch and work on the idea or bug fix on that branch. This doesn't touch our existing code at all. And then, when you're ready, you can pull those changes from the one branch back onto the main branch. So let's go ahead and see how this works. So I'm here in a repository, and I'm gonna start, as always, with a git status, and I want you to notice something special about one of the lines of output we get here. Right here, you can see we've had this line all along, and it says On branch master. This means we're working on the master branch. Now, the master branch is the branch that git creates for us by default. There's nothing special about the name master, but that's just what it calls it by default. The master branch is where we've been working this whole time, a single branch of this git repository the whole time. So, now let's say that we have some special feature that we want to develop, and currently our master branch is in a stable position, so we don't want to accidentally break something there while we're developing this experimental feature. So what we wanna do is create a branch, and work on the experimental feature on that branch. So it's very easy to create a branch, we use the git branch command. And first I'm just gonna run that without any options or parameters, and then you can see that we have this output here. We have master, and it's in green and we have an asterisk in front of it. This is a list of the branches we currently have and as you can see, we only have one, master. And the asterisk in front of it says that this is the branch that we're currently on. So we are currently on our only branch, master. All right, so let's say we want to create a branch. I'm going to run git branch and I'm going to pass it the branch name, and we're going to call this experimental-1. Now when I hit enter it, doesn't look like anything happened, but if I run git branch again, you can see that we have two branches listed here, experimental-1 and master, and we're still on master. So running git branch and passing it a name for a branch doesn't actually put us on that branch, all it does is create the branch. So what we want to do now is move to the branch, and to do that, we need to use the check out command. Now, the git checkout command is extremely versatile, and is used for a lot of things. But right now, just know that when we pass a branch name to the git checkout command, we are actually going to switch to that branch. So I'm going to say git checkout experimental-1, and now, you can see we have this message here, switched to branch experimental-1, excellent. So now, we are on the experimental branch, and if I run, git status, you can see that our message here has changed on branch experimental-1. And there's still nothing to commit, cuz we haven't made any changes in the working directory of our current branch. So let me see, let's make some changes to the to do file for now. I'm going to say learn about branches, and I'll mark it as done right away, just because we can. And I'll save and close that, and now if I run git status, you can see that that's been modified, all right. So I'm going to run git commit -am to commit a file that's previously been committed. We'll just say added a branch todo item. So now that we've done that and our working directory's clean, I'm gonna move back to our other branch. And I'm gonna go git checkout master, and this, as you can see, switched me back to the master branch. So now, let's open that to do file in here. As you can see, the to do file has not been changed at all because this is on the master branch, and we made those changes on the experimental-1 branch, nothing on the master branch has changed. Now this is the very same file, but it doesn't have the same content because remember, git tracks content, it doesn't specifically track files. So it's tracking the state of the file, and this is the state the file was in during our last commit on the master branch. Now, I want to point out something else here. We made sure that we committed those changes before we switched back to the branch, but let's open up the README here and I'm gonna add, let me just add the year to the end of this, and actually I will add the month and the year. Now if we were going to git status, you can see that we have a modified file there. But I can do git checkout experimental-1, I can switch to this branch. Notice that we have a little message that M README, so README has been modified. If I run git status here, you can see that those modifications are still showing up here on the experimental branch, and that's because they have not been committed yet. These are in the working directory still, and when content hasn't been specifically committed, it doesn't really matter what branch we were on when we created it because that content isn't specific to the branch until we commit it to the branch. So here I could do git commit -am and just say, added date to read me, I can commit that. Now if I run git status, you can see that our experimental branch is clean. I can git checkout master and run git status, and as you can see there is nothing to commit. And if I cat out the README, you can see that we do not have the date there. So even thought we made those changes on one branch, git doesn't actually see that as have being made on that branch until we commit it to the branch itself. So branches are really, if you think about, that simple. I've shown you how to create a branch, and how to switch between branches. Other than that, for most of your everyday working with git, that is really most of the heavy lifting of what you'll have to do with branches. You make them, you switch back and forth between them, and you commit stuff and do other things that you're already familiar with doing while you are on those branches. Now, of course you're gonna get to the point where you actually want to pull content from one branch to another branch, and we're gonna look at that on our next video. But right now, I want to show you a shortcut for creating branches. Let's say we want to fix a bug, so we want to create a branch and do that off on a branch. So right now we are on the master branch, okay. So I'm going to say git checkout -b. Git checkout -b means we're going to checkout to a branch that doesn't exist yet, so create the branch. We'll call the branch bug-fix-1. Now as you know, the branch bug-fix-1 does not yet exist. And for example, if I take out that b, I'm going to get an error here. You can see the pathspec bug-fix-1 did not match any files known to git. So git says well, you're telling me to check out bug-fix-1, but I don't know what that is. So I'm gonna pass it the -b flag so that it knows this is a branch that we want you to create. If I could spell git correctly, you'll see we have the text here, switched to new branch bug-fix-1. Excellent. Notice that it didn say new branch here instead of just switch to branch, as it does before. So it's just a little indicator that we actually just did create this branch. So now, we are on the bug-fix-1 branch. So let me show you something else that is really neat that you might not realize. I'm going to open a, we'll just call it fix.txt, and this is our bug fix. Now we'll save and close that. If we run git status, as you can see, we have an untracked file so I'll git add everything and then git commit, and just say we fixed the bug. And I have to remember not to use the aliases that I normally do that I don't have set up. And now, if we run git status, you can see that our working directory is clean. So what I'm going to do is open up this folder in finder, because I think it's fun just to watch this happen the first time to see exactly what's gonna happen. Over here in the terminal window, I am going to run git checkout master. Now I'm about to hit enter, so I want you to watch right over here as I hit enter. Watch that fix.txt file. I hit Enter and it disappears just like that. Where has it gone? Well, you'll remember that content was created on our bug fix branch. It wasn't created on our master branch. So the master branch doesn't have any knowledge of that content. So when we switch back to the master branch, the file disappears because it doesn't know about the blobs that hold that content, and its tree object doesn't have an entry for that fix.txt file. So that's something that I find really, really cool, and something that it takes a bit of thinking about to get your mind around. Branches track the content separately, and it actually changes the actual content of the directory, of the working directory, when you switch between a branch. That content, for all intents and purposes, doesn't actually exist anymore until we switch back to that branch. So those are the basics of using branches. And you will remember from the previous video, we talked about git log and how you can log a graph to see the commits that have been made on different branches and kind of get a visual representation of that. I'm going to show you how to do that right now. So as you know from last time, we can do git log --oneline, and that gives us a list here of all of the commits that have been made on this branch. Now if I update this to include the --graph flag and I run that, you can see that we're still only seeing the commits that have been made on this branch. And so to see all the commits from all branches, I'm gonna add the --all flag. And when I run that, now you can see the commits that have been made on all the branches. As you can see right here, this is where things start to branch. This one down here added README. This is the last commit that was made on the master branch. Now I know that because I remember that, not because there's anything here to tell me that. And we'll look at how to add that in just a second here. But then you can see we branched off here, and this over here is our experimental-1 branch, we've made two commits on that, and those are off to the side here. And then straight up, We've made the fixed the bug commit, which was the commit that we made on our bug fix branch. So let me clear the screen here and bring that command back up. So we have git log --oneline --graph --all, and then I want to add one more flag here, --decorate. And this is a really nice representation. This adds the reference names to our tree, and we can see exactly which commit each branch currently points to. And if you remember back to our theory lesson where we talked about references and how these different references point to specific commits, you can see here that the head reference points to the latest commit on the current branch. And since we're on the master branch, that is pointing to the same commit that the master branch is currently on. Up here, this commit here is where our experimental one branch is, and up here we have the bug fix branch. And just to show you how that works with the head there, if I run git checkout bug-fix-1, I switch to the bug fix branch. And now if I run that same command, you can see that the head reference is now at bug-fix-1. Of course we can run gitk --all and we can get another type of view of this, as you can see here. Now we can see where things have branched off, and this has master as the tag there. Then we have our branch here, experimental-1 branch, and then up the top here we have our bug fix branch. And you might think in both of these representations, it really looks like our bug fix commit is on the same branch, so let me do this to show you that it's not. We'll check out the master branch again. Let's make a change to the README file. I'll open up the README file there. I'm just gonna change the title here and say a Tuts+ Premium Course. So I'll save and close that. I won't even go to git status, I'll just do git commit -all message updated README title. And now if I clear the screen and run git log, you can see that our bug fix branch actually does branch off separately. So we have our master branch going all the way up here, and you can see now right at the top we have head and master, and then the bug fix branch is off this way, and further down we have the experimental branch branching off. So that is the basics of using branches. However, as I said, sometimes you're gonna want to pull content from one branch to another, and that's what we're gonna talk about in the next video.