![Git In-depth](https://static.frontendmasters.com/assets/courses/2018-02-20-git-in-depth/thumb.webp)
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
[00:00:00]
>> 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.
[00:00:30]
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.
[00:00:54]
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.
[00:01:21]
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?
[00:01:43]
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.
[00:02:06]
>> 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.
[00:02:27]
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?
[00:02:42]
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.
[00:03:02]
>> 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.
[00:03:33]
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.
[00:03:50]
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.
[00:04:21]
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.
[00:04:52]
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.
[00:05:17]
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.
[00:05:39]
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.
[00:05:59]
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