In this movie, we're going to learn about a very powerful tool in Git that
allows us to undo multiple commits.
And because it's powerful, it's also very dangerous, so I want to say right off
the bat that you need to use this with extreme caution.
The command that we're going to be using is git reset.
What git reset does is it allows us to specify where the HEAD pointer should point to.
Normally, we just let Git manage the HEAD pointer for us.
We make a commit and Git moves the HEAD pointer to point to that commit, we make
another commit, and it moves the HEAD pointer to point to that commit.
Well, here, we're telling Git, "I want to be in control, I want to move the HEAD
pointer over here, and that's where you're going to start recording from now on,
that's where you're going to start making your commits."
It's a lot like if you think back to the metaphor that I gave you early on about
the cassette tape recorder.
Remember that I said that the HEAD pointer is a lot like the playback and record
head on the tape recorder.
Let's imagine that we've recorded an hour's worth of audio.
If we press Stop, now if we start recording again, of course it starts recording
right there at the end of that hour's worth of audio.
But what if we instead rewind back 10 minutes into the audio?
Now, if we were to press Record, we would record over that last 10 minutes of audio.
It would be very destructive. You can see why git reset is powerful.
It does the exact same thing.
It says let's rewind back to our previous commit, and that's where we're going
to start recording from now on, and we're going to just overwrite whatever came after that.
Git reset always moves the HEAD pointer. That's one thing that it does in every case.
But there are three different options that I want us to look at that we can use
to control some of the other behaviors that it has, and those options are going
to be soft, mixed, and hard.
Soft is going to move the HEAD pointer to the specified commit, and it's not
going to change the staging index, or the working directory at the same time.
It's just going to move the pointer.
It's the safest of all these options, that's why it's called soft.
Just a soft reset, move the pointer and do nothing else.
The result of that, if we've rewound backwards is that our staging index and our
working directory are going to contain the files in their later revised state.
The repository is going to be set back to an earlier version.
So, if we do a diff between the two, it's going to tell us about all those
changes that have happened between the point where the HEAD is pointing, and all
the files that are sitting in our staging index and working directory.
So, that's a soft reset. That's the safest one we can do.
The second one is a mixed reset.
This is in between soft and hard which is why it's called mixed, and it is the default.
What it does is it moves the HEAD pointer to the specified commit, and it also
changes the staging index to match the repository.
It does not change your working directory though.
So, at this point, the staging index and the repository will be set in one
place, our working directory, though, has all those changes that we've made.
All the things that were in later versions of the repository are still in
our working directory. We haven't lost any work.
It's just waiting for us to stage it and then commit it.
And then the last one is the most destructive of all, and that is hard.
A hard reset will not only move the pointer of the repository, but it will make your
staging index and your working directory match that as well.
So that means any changes that came after that commit are completely obliterated.
They don't exist in the repository, the staging index, or the working directory,
they're completely gone. So use this one with caution.
It really is for when things have gone completely wrong, and you really just
want to reset everything back to a specific point in time, and you don't mind
losing whatever came after it.