Git In-depth

Git Checkout

Git In-depth

Check out a free preview of the full Git In-depth course

The "Git Checkout" Lesson is part of the full, Git In-depth course featured in this preview video. Here's what you'd learn in this lesson:

Nina reviews git checkout command, which allows for resolving mistakes by restoring working treefiles or switching branches.


Transcript from the "Git Checkout" Lesson

>> Nina Zakharenko: The next two sections are some of the most important ones. They're going to help you in your work day to day.
>> Nina Zakharenko: The very first one, question everyone wants to know is how do I fix mistakes? There's a few tools that we use, checkout, reset, revert. They're kind of confusing and some of them work similarly, in slightly subtle ways.

They take flags or commits. After this section you're not gonna have a hard time figuring out which command do you need for what scenario. Because fundamentally in order to understand how to fix mistakes, means having a really clear picture. A clear idea of how those three areas where code lives work.

The working area, the staging area, the repository, and how we can move data between them. The very first way that most people learn how to fix their mistakes is with git checkout. It allows you to restore working tree files or switch branches. And the action that is taken depends on the parameters.

Checkout can move that head pointer and update the staging area and the working directory. If you're checking out a file, it can perform the same actions without moving the pointer. A key thing to remember here is remember how earlier in the course we talked about how the staging area isn't empty?

It's a copy of the current commit. It has all the files that are in our current commit and their SHA. This is a important concept to remember when using git checkout. So when we git checkout a branch, here are the steps that happen. The first thing we do is change HEAD to point to that new branch.

>> Nina Zakharenko: We were on master, now HEAD is moving to our tech post branch. The second thing we do is copy the commit snapshot to the staging area. Remember, the staging area can also be called the index. So if you're reading documentation later, that's a good thing to keep in mind.

So we copy it from the repository to the staging area. The last thing we do is we update the working area with the branch contents. Any changes to files in your working area are kept. Remember how I try to check out a different branch and git warned me?

It said that my files would be overwritten. Changes in the staging area are also capped unless they conflict with any changes in a branch you're about to check out. Checking out a branch is generally a pretty safe operation. Nothing bad's gonna happen, git's gonna warn you if you're gonna lose data.

>> Nina Zakharenko: Now, what happens when you get checkout -- a file?
>> Nina Zakharenko: What it does is replaces the working area copy with the version from your current staging area. Git recommends this command to run when you want to remove a file. Replace the file contents in your working area with the clean file contents from the repository.

So in this case, it's going to copy the content from the staging area to the working area. One thing to note, this is really important, is this command will completely overwrite your files. It's gonna overwrite your files in the working directory. If you have any changes to them, and you run this command, that's it.

They're gone, they're blown away and there is no way of getting them back. I'm always kind of surprised that git suggests this action in the messaging, because it's a disruptive operation. And if you don't know what you're doing, you can really mess up your work. When git checkout overwrites files from the staging area, it's gonna overwrite the working area file with the staging area version, the one from the last commit.

So very, very careful. One thing I wanted to talk to you about is, a lot of people get confused about what those two dashes mean. When I say get -- checkout, this signifies the end of a command operation and the start of positional parameters. So if I had the same, if I had a branch and a file with the same name, git checkout could possibly be ambiguous.

So with the -- we're specifying that the next argument is a file in this case and not a branch. Now what happens in this third scenario? You git checkout, you specify a commit, and then -- a file. It updates the staging area to match the commit. And then it updates the working area to match the staging area.

Again, this operation overwrites, it will overwrite files in your staging area that are staged. It'll overwrite files in your working directory. When you're running git checkout and you have unsafe changes, it's time to stop, pause, stash, commit them. Do whatever you need to do to get your code in a safe place because that command is very destructive.

When we checkout from a specific commit, again, this copy is to both the working area and the staging area. Using this process we can restore a deleted file. If we git checkout we find the commit where we deleted that file, and we specify hat to point to its parent commit.

We can pretty easily checkout that deleted file and get it back.
>> Nina Zakharenko: Again, big warning. Putting a big warning on all of these slides.

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