In this chapter we're going to talk about how you can undo changes in Git, and
that can be changes that you make to your working directory, changes that you've
staged into the staging index, or even changes that you've actually committed
all the way to the repository.
It's very common that you say, "Oh you know what? I want to undo what I just did,"
and Git can help us do that.
In this movie we're going to start by looking at how we can undo changes that
we've made to our working directory.
So let's say, for example, that we're working with our project, and inside
Export California index page we open that up, and this whole site navigation
here, where do you want to go, all of this--scroll down until I get to the
bottom of it, here it is-- the bottom of that div, we delete.
Maybe it was on purpose, because we we're thinking about to deleting it, or maybe
was just an accident. We didn't mean to delete it.
But we save the change to the file, and then we close it.
Now that file has been changed.
If we go to Firefox, and we open it up, we reload the page, the
navigation disappears. Well, that's not what we wanted.
That was a mistake. So how do we get that back?
If we go to Git, and we say git status, it shows us that the file has been modified.
If we do git diff, it comes up and it tells us what the change was.
The change is that all of this text has been removed.
You see all of the minuses next to it.
So what we want is we want the repository version back.
We want the version that Git has saved for us to be restored.
It's using that version to compare when it does the diff.
What we're going to do say, "Git, go back to the repository, get that version, and
check it out for me and replace what I have in my working directory with it."
And to do that, we use the git checkout command. You could say git checkout index.html.
I think in a lot of cases that will work.
However, the thing about checkout is that it's used for more than one purpose.
It's also used for working with branches.
Because what checkout does is go to the repository, get the named thing that
I've gave you, and make my working directory look like that.
That's what it does.
So if that named thing is a branch, it brings the branch down. If that named
thing is the file, it brings the file down.
Imagine for a moment that we wanted to bring down this resources folder.
So we say git checkout resources.
Well, that's fine, but what if we also have a branch named resources?
Then it's hard for Git to tell which one we need and in that case it actually
would give us the branch resources instead of the folder resources.
So as a result, it's a good practice when we're not trying to checkout a branch
to put dash--dash, followed by index.html that says stay on the current branch.
That bare double dash is just there to indicate that we're not checking
out a new branch, we're just talking about a file in the current branch.
We'll talk more about switching branches later, and we won't use the double dash in that case.
But here we want to use it to say stay on the same branch, go get this file, I
don't care what's in my working directory, I want to make my working directory
index.html file look like what the repository has.
So let's hit Return.
You can see it changed there briefly in the background as this file went away
and then came back again.
Before we even go look at the file, let's just come back here and reload the web page.
Sure enough, our working directory now has the menu again.
The menu shows up, and if we pop back over here to Git, git status, the working
directory is clean again.
It went and got that version and completely threw out our working directory changes.
That's all there is.
Git checkout with a double dash and then the file or the directory that we
wanted to pull down from the repository will blow away the changes and restore
them back to what we had in the repository.