Check out a free preview of the full Everything You'll Need to Know About Git course

The "Branching" Lesson is part of the full, Everything You'll Need to Know About Git course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen introduces the concept of branching in Git and explains that branching allows for separate development and experimentation without affecting the main line of code. He also demonstrates how to create a branch, switch between branches, and view the branch details.

Preview
Close

Transcript from the "Branching" Lesson

[00:00:00]
>> We're gonna go into Git Branching. First off, you should not develop everything on the main line, okay? That'd be kind of crazy if all you ever did is just simply use whatever your main line is and just put all of your changes on that. But I'm gonna be a little honest with you guys, when it's just my project, I only develop on main, and that's it.

[00:00:18]
That's all there is to it. But if I'm working with somebody else, you should branch, branches are pretty much cheap, they're almost free. So you can create as many of them as you'd like, and it's really not gonna create a problem for you, for your system, for memory.

[00:00:33]
Again, every branch will be pointing to a commit, which will contain the entirety of your repo, but they're free, almost at the exact same time. We'll find out why. Since we're on a new repo, let's create our initial commit, cuz remember, whenever you start a repo, there's no commits in there.

[00:00:47]
So it's empty, you can't even create a branch right now, things are all goofed up. So let's just create a quick change. And I want you to have a readme.md with one line, put a in there, and then with a commit message of a. It's very important that you use these commit messages more than anything else, because if you don't, you will probably get a little bit confused with the course.

[00:01:06]
So I'll do it too, readme.md, I'll put in a nice little a, git add this git status git commit. By the way, always use status before you commit. Make sure you don't actually say commit an out file by accident, while teaching a course on Git and showing people how to properly use it.

[00:01:23]
And of course, message A because we wanna keep track of this, okay? So there we go. You can do a little quick git log and see, look at that. Hey, fantastic. All right. So let's create a branch. And we're gonna call this branch foo, why? Cuz I say so.

[00:01:42]
I know some people hate foo and bar as example things, I love foo and bar, okay? I don't know why everyone has to hate foo and bar, it's fantastic. All right, so git branch foo, this is how you create a branch. We just created it right now. Was this your expectation when you create a branch?

[00:01:59]
Yes or no?
>> Yes, but I'm used to it.
>> Yes, cuz you're used to it. That's actually the proper answer right there. If you're new to Git, that was a weird experience. You just created a branch and nothing happened, right? You're just still sitting there, you're on trunk, where am I?

[00:02:14]
Why did this happen? I wanna go home. Well, guess what? You already are at home. The thing is that, when you create a branch, it creates a branch at the commit you're currently on but it does not switch branches. There's actually a couple ways you can, first off, you can check out any branch you have, so I can actually execute git branch.

[00:02:33]
You should do this right now. Use git branch, check it out. I talk about log as well, so you can go get branch, check that out, you see, I'm still on trunk, but I do have a foo now. If you go get log, you'll see, head points to trunk, that's where I'm at, but there's also foo, foo's at the exact same place.

[00:02:54]
Cuz remember, when you create a branch, it branches off wherever you're at. At this moment, very important. Some people only know that by accident, they haven't really ever thought about it, but yes, it's literally pointing to the same commit. Let's do a little bit of Git Foo. Can you find your branch details in .git?

[00:03:15]
See, look, take a moment. Explore
>> Refs, heads, foo.
>> I'll give you a little hint, go check out refs. So I'll go like this, git/refs/heads/foo/trunk, look at that. So I can actually cat that out. Hey, that's my SHA. The branches just exists in this heads folder as just a SHA.

[00:03:46]
So that git remember, what do we do with a SHA? When I cat a file of a SHA, right, I go git cat file-p, it then shows my tree, right? So this is how good Git kind of understands and is able to build the state of your repo based off of a branch.

[00:04:04]
Is a branch is just a SHA, a SHA is the state of your repo, it's not a differential, that's why it's always able to do it and do it so fast. That's why branches are effectively free, is it's just 40 characters in a file. And name, so it's like 45 characters for our case.

[00:04:20]
Anyways, so if you want to switch branches, you can actually do git switch or checkout. I'm just used to using checkout, so I just use checkout. You can use switch if you want to. They're effectively identical for the standard cases, I read on the Internet, so it must be true that checkout is more versatile.

[00:04:36]
But I only use checkout to create a branch or to switch to a branch or to do something with merge conflicts, which we'll talk about later. So pretty much, I just use it for the most basic operations. And so, switch the branch foo, so I'll go git check out.

[00:04:54]
Foo. Look at that, we're on branch foo, how fantastic. Its beautiful, right? There we go, it even tells you, you've switched to branch foo, awesome. All right, so now let's actually add some commits to foo. We're gonna add two commits, B and C, make sure you actually use B and C cuz it makes everything easier to use that.

[00:05:13]
And I have a small typo right here saying, add the changes to README. Don't add them to README, add them to a new file. I called the file second, and so you can do the same thing. All right, so I'll go echo B into seconds, remember that. Git add it, git, commit it, B, and I'll do it again with C.

[00:05:38]
Fantastic, now I have two commits into foo. We'll do a git log just to prove it. ABC right here, awesome. I'll give everyone a moment to catch up. All right, so you should now have the repo set up like this. A should contain trunk, B and C should be foo.

[00:05:55]
We can go get log, just verify that trunks at A, foo is at C, awesome. Foo C. There we go, this is what I pretty much executed right here, we added those two things, and when we do the log, you can see trunks down here. Foo is up here.

[00:06:11]
Remember, your commits won't match my commits, totally normal. Again, totally based off of time, author, everything. Remember, you can delete branches, I don't know if you know that, you do the D command. We will actually use it later, I'm just putting it in here. Just remember -D deletes a branch, I always use D because I don't want it to have to try to check against the origin to see, can I delete this branch?

[00:06:34]
Is it up to date with what I'm tracking against? We'll go over tracking and all that later on. But I just use D whenever I'm deleting something, old habits. All right, so now we have a branch full of changes, what do we do next?
>> Merge
>> Merge, did I hear merge?

[00:06:53]
>> [LAUGH]
>> Absolutely, we gotta merge it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now