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.

Git Checkout and Intro to Branches

Give Feedback

In this lesson, you’ll learn how to go through git commit history to checkout old commits. You’ll also learn what the terms master, HEAD, and branch mean.

00:01 You’re going to like this. Git has another cool feature. It can travel forward and backwards in time. While we can’t quite go back and see a T-Rex, you can move forwards and backwards in the timeline of your project.

00:14 This is done with a command called checkout. In this module, you’re going to learn a couple of new and important terms named HEAD and branch, so let’s hop into the console and see if you can spot the terms.

00:27 Then we’ll define them. Let’s make one more commit before we start working with git checkout, and let’s just commit the rest of The Zen of Python to our zen_of_python file.

00:40 So hop back into your Python console, grab the rest of The Zen of Python—you stopped on line 7, somewhere around here. Just grab the rest of it, copy it, hop back into your repo, open your zen_of_python file. If you’re using VIM, hit i for insert, hop down to the next line, and paste everything in.

01:05 Then Escape, Shift, colon (:), wq, Enter. And now if you take a look at zen_of_python, it should be complete. So let’s commit that. Remember how to commit?

01:21 git add zen_of_python, git commit -m, and put our imperative message,

01:34 and commit the file.

01:40 Check git status, and you’re ready to go. So the first thing you want to do is run git log, and you can see our three commits that we have in this project so far.

01:52 So let’s move back to this old commit. To do this, you need to use the git checkout command and specify the checksum of the commit that you want to view. git checkout,

02:13 and we’ll move to this one, copy it.

02:20 Well, that just blasted you with a bunch of information, huh? That’s the kind of message that makes you sweat a little bit. Let’s small chunk this, so we can kind of digest this message. First, Git verifies that it is checking out the commit.

02:35 Then it tells you you’re in a 'detached HEAD' state. What does that even mean? And there’s another new term in there called branch. I wonder what that means.

02:44 And from this message, we see that we can make experimental changes in this quote-unquote 'detached HEAD' state. And it tells us if we want to retain commits, we’d need to create another branch.

02:58 So let’s get our HEAD back on and we’ll take a look at a diagram to kind of walk you through what just happened. So, to reattach our HEAD, we’re going to do git checkout master.

03:12 Now let’s go check out a diagram. Check this out. You’ve been working in what’s known as a branch the whole time, and it’s called the master branch. When you initialize your Git repo, it creates the master branch automatically.

03:28 So, here’s the master branch with each of these dots representing our three commits. If you wanted to work on a new feature, you might start your own branch.

03:40 Branches are integral to working in Git. They represent independent lines of development. You use branches to diverge or separate different lines of thought.

03:51 Maybe you want to experiment or work on a new feature and you don’t want to affect your master branch. You can use these branches to do so.

04:00 So, going back to the earlier example from our console, you got a warning that you were in what’s called a 'detached HEAD' state. HEAD is just a pointer indicating where you are within your branches and/or commits within the project.

04:16 Typically, the HEAD sits at the end of whatever branch you have checked out. The first time you ran checkout, Git moved HEAD to point at the first commit and it was warning you that you weren’t in the most current area of development for the master branch.

04:32 So this detached state is good for traversing and viewing the timeline of a project, but we shouldn’t use it for writing new code and features, especially when working on a team. For that, we’ll start using branches, and we’ll discuss them in the next video.

Dennis Smith on Dec. 3, 2020

If anyone caught a snag on git checkout master like I did, it’s because it has been replaced with git checkout main to avoid referencing slavery. Read more about it here: github.com/github/renaming

Bartosz Zaczyński RP Team on Dec. 4, 2020

Good point. It’s worth noting, though, that master is still the default name in git when you create a new repository on your local machine. It’s GitHub that recommends renaming your branch to main with the following command, which isn’t mandatory:

$ git branch -M main

So you can still work with older repositories or keep the traditional branch names in new ones if you wish.

Become a Member to join the conversation.