Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Working With Git Branches

Give Feedback

Learn the basics of branching in git. After watching this video, you’ll know how to create new branches and make changes to them. You’ll learn how to:

  • Switch to a new branch using git checkout
  • Add commits to a new branch
  • Compare branches using the git show-branch command

00:01 So, you have a theoretical understanding of branches from the last video. If you skipped the last one, you should go back and watch it or else this one might not make sense. Often when you’re working on a project, you’ll want to work on a feature or test something without affecting the master branch. So this is a great time to start using branches.

00:19 Let’s hop into the console and make a new branch. This is done with the checkout command. So go ahead and type out this command with me, git checkout, -b for branch, and then you have to give your new branch a name.

00:37 Go ahead and call it whatever you want. I’m going to call it my_new_feature.

00:43 And you can see that our parenthetical has changed. Let’s check out git status. That tells you you’re on your new branch my_new_feature, you have nothing to commit. And if you run git log, you can see that you’ve got all the commits to the master branch up until you’ve branched off to start your new features. Going forward, the new feature branch will have different commits than master.

01:09 So let’s make a change to one of our files and commit it to the new feature branch. And this will simulate adding a new feature. So let’s go ahead and change zen_of_python. Actually, let’s just go ahead and add a new file.

01:28 So, why don’t you to call it my_new_feature.

01:35 Go ahead and put some text in here. i for insert.

01:42 And when you’re done writing, the Escape button, Shift plus colon (:), wq for write quit, and Enter. And now if you do ls and list your files, we’re going to have a new one called my_new_feature.

01:58 So we’ve got a new file added on our my_new_feature branch. So let’s go ahead and check git status, then go ahead and add that file,

02:17 and commit it.

02:22 Give it your imperative commit message.

02:32 Now let’s jump back into the master branch, and we’ll use git checkout to do that. So I hope you’re starting to see that checkout, that command, is your means of traversing through both branches and commits.

02:48 And run git log. So that commit you just made is not present on master. Why’s that? Well, it’s committed to your new feature branch and the master branch is exactly as you left it. If you ever want to compare branches, you can do that with a command called show-branch.

03:08 And that looks like this, git show-branch. And then you’re going to input the two branches that you want to compare. So let’s compare my_new_feature and our master branch.

03:27 The output from this command shows the branches and their most recent commits in the top two lines. Then there’s a -- separator and then it shows the commits in each branch.

03:39 This is a confusing view at first, but treat the first two characters or spaces as columns, and this will indicate when a commit is in one or both branches.

03:50 This is a useful command for finding what’s known as common ancestors—commits that are in the history of both branches. So, what happens if you’re done with your feature and you want to bring it back into your master branch? That’s called merging or rebasing, so let’s take a look at those two commands in our next video.

Become a Member to join the conversation.