git reset is the command that allows you to shape history in your repository.
Whether you're undoing some changes
or crafting your commits differently,
this is the command for you.
(bossa music)
Welcome to the episode of Git and GitHub Foundations on the reset command.
This command has a lot of power,
and a lot of modes.
Yeah, there's going to be three modes
we're going to be working with here.
There's going to be soft;
mixed, or the default; and hard.
(music)
these adjectives are pretty descriptive,
starting with the one that's the default, mixed.
It alters both the history and the working directory.
So mixed, both things.
And soft is going to take one or multiple commits
and put all of their changes back into the staging area
to allow you to continue to craft
a new commit out of that.
Hard, on the other hand, is a destructive operation
that's meant to erase things that you no longer wish to keep.
So with all three of these in our stable,
let's look at mixed first.
Mixed is the recommendation that most often gets shown to new Git users
because it shows up in the status command.
When you have changes in the staging area
and you say git status,
we see a Git reset head.
That allows us to take those changes
out of the staging area
and put them back into the working directory
with the mixed option.
Soft is the way that I take some changes
that might have gotten too granular in nature.
One, two, three, four, five commits
that I then realize all belong
in one transaction,
and to bring them all together
by a git reset --soft HEAD~5
the most recent five commits.
So with that command of using soft
you can take all five of those commits
and kind of squash them down
into the staging area as one commit.
Pretty useful for reshaping history,
but reshaping history goes to the far end of the spectrum
to throw away commits that just don't add value.
You don't want to send them, transmit them, or share them with others.
That's where hard comes into play.
If you completely want to throw away some of your work,
maybe you're trying out some change that just didn't work out,
you can use the git reset --hard to throw those commits away completely.
People might ask, then why would you make a commit?
But Git provides you a really nice safety net
through the reflog command that we'll look at in a future episode
that motivates you to make checkpoints with commits,
and knowing that you have hard at your disposal,
you can clean that history up anytime you like.
(music)