In the previous movie we saw how we can use the amend option with commit in
order to edit our very last commit, the commit that the HEAD still points to.
We also saw how it's difficult to amend older commits, because that would
violate the data integrity, which is an important feature of Git.
Instead, if we need to make changes to those older commits, the best advice is
to make new commits, commits that undo what was done in those older commits.
Not only does it maintain the data integrity of Git, but then the log file also
accurately reflects the changes that were made over time, and it shows that a
1.1 change was made and then several commits later another commit was made.
That information might actually have some importance, especially if the commits
in between played off of those changes in any way.
Now, what it doesn't do is allow us to hide our mistakes.
Instead, you'll just have to record the mistake you made and then record the
fix that goes with it.
In order to make new commits that undo changes, there's a number of different
ways that we could do it, one would be we could simply manually make those
changes just like we did the first time and then commit the result.
But I want to show you another way that we can do it that's a little bit faster too.
Let's take a look at our git log, and let's take a look at this last commit that we made.
Now we could use amend to just edit this commit.
But this is the one where we change our resources.html file.
We rearrange the items that we're going to list there.
Let's say we made two or three more commit since then and we wanted to go back
and we wanted to rework this file and get it back to its old state.
What we can do is we can check out this file, check out the old version of
resources.html from before you made that change.
So, if the change was made in this commit, then the commit that has the version
previous to that is this version, and probably all the versions that are before.
But we'll take the one that's immediately before.
And what I'm going to do is I'm going to copy part of this S-H-A at the top.
Now, you could take the whole thing, but it's not necessary, these numbers are
unique enough that you can usually just grab the first 10 or so characters and
copy those and use those for a reference.
And then I'm going to say git checkout, I'm going to use that reference to
the commit that I want followed by a dash, dash, that's that bare double dash
that we used before with checkout to let it know that we're not talking about
a branch, we mean the current branch, and then the file that I mean, resources.html.
So now I've told it to check that out.
Let's take a look at git status, and you'll see that it moved it into my index.
So I've checked it out and here it is ready to be committed.
Now, this behavior is slightly different. Before when I did a check out, it put
up my working directory. Here it went ahead and put up my staging index.
When you check it out from our particular revision, it puts it into your staging area.
Now, if we do git diff --staged, we can see the comparison, and we can see that
this is the old file, the old version, and if we were to commit it, it would revert those changes.
It would change it so that sunglasses, sunscreen, insect repellent move from the
top back down to the bottom where they were originally.
So there it is ready to be committed, I can just do a regular commit now, and it
will undo that change that I made previously.
It's a good habit when you're reverting a particular commit to actually refer to
the SHA for that commit.
So, it would be a good idea to take part of the SHA and copy it and then say
this commit reverts commit, and give that SHA, so it's a reference.
So people can look it up and figure out what you were trying to revert.
We could go ahead and commit that, but I'm going to instead bring up the status again.
Let's do git reset HEAD on resources.html.
That will put it back in our working directory, and then we'll do git check out --
resources.html, and that now removes it from our working directory so that now
our working directory is clean.
And the reason I don't want to do it here is because in the next movie, I'm
going to show you how we can use a command called git revert to do something very similar.