WEBVTT
00:00:00.206 --> 00:00:04.306
Git reset is the command that allows you
to shape history in your repository.
00:00:04.306 --> 00:00:07.945
Whether you're undoing some changes
or crafting your commits differently,
00:00:07.945 --> 00:00:09.401
this is the command for you.
00:00:09.651 --> 00:00:11.241
♪ (bossa music) ♪
00:00:14.691 --> 00:00:17.130
Welcome to the episode of Git
and GitHub Foundations
00:00:17.130 --> 00:00:18.669
on the reset command.
00:00:18.669 --> 00:00:21.510
This command has a lot of power,
and a lot of modes.
00:00:21.510 --> 00:00:25.002
Yeah, there's going to be three modes
we're going to be working with here.
00:00:25.002 --> 00:00:28.236
There's going to be soft;
mixed, or the default; and hard.
00:00:28.555 --> 00:00:30.587
♪ (music) ♪
00:00:31.162 --> 00:00:33.101
These adjectives are pretty descriptive,
00:00:33.101 --> 00:00:35.513
starting with the one
that's the default, mixed.
00:00:35.830 --> 00:00:39.648
It alters both the history
as well as the working directory.
00:00:39.918 --> 00:00:41.708
So mixed, both things.
00:00:41.898 --> 00:00:44.427
And soft is going to take
one or multiple commits
00:00:44.427 --> 00:00:47.179
and put all of their changes
back into the staging area
00:00:47.179 --> 00:00:50.048
to allow you to continue to craft
a new commit out of that.
00:00:50.433 --> 00:00:53.039
Hard, on the other hand,
is a destructive operation
00:00:53.039 --> 00:00:55.992
that's meant to erase things
that you no longer wish to keep.
00:00:55.992 --> 00:00:59.112
So with all three of these in our stable,
00:00:59.112 --> 00:01:00.710
let's look at mixed first.
00:01:03.475 --> 00:01:05.173
Mixed is the recommendation
00:01:05.173 --> 00:01:07.241
that gets most often shown
to new Git users
00:01:07.241 --> 00:01:09.419
because it shows up
in the status command.
00:01:09.419 --> 00:01:11.341
When you have changes
in the staging area
00:01:11.341 --> 00:01:14.103
and you say git status,
we see git reset HEAD.
00:01:14.103 --> 00:01:16.926
That allows us to take those changes
out of the staging area
00:01:16.926 --> 00:01:19.015
and put them back
into the working directory
00:01:19.015 --> 00:01:20.201
with the mixed option.
00:01:20.672 --> 00:01:23.379
Soft is the way
that I take some changes
00:01:23.379 --> 00:01:25.706
that might have gotten
too granular in nature.
00:01:26.033 --> 00:01:27.869
One, two, three, four, five commits
00:01:27.869 --> 00:01:30.846
that I then realize all belong
in one transaction,
00:01:30.846 --> 00:01:32.434
and to bring them all together
00:01:32.434 --> 00:01:36.542
by a git reset --soft HEAD~5
00:01:36.542 --> 00:01:38.310
the most recent five commits.
00:01:38.615 --> 00:01:40.920
So with that command of using soft
00:01:40.920 --> 00:01:42.706
you can take
all five of those commits
00:01:42.706 --> 00:01:46.285
and kind of squash them down
into the staging area as one commit.
00:01:46.665 --> 00:01:48.765
Pretty useful for reshaping history,
00:01:48.765 --> 00:01:51.465
but reshaping history goes
to the far end of the spectrum
00:01:51.465 --> 00:01:54.407
to throw away commits
that just don't add value.
00:01:54.407 --> 00:01:57.748
You don't want to send them,
transmit them, or share them with others.
00:01:57.752 --> 00:01:59.411
That's where hard comes into play.
00:01:59.411 --> 00:02:01.983
If you completely want to throw away
some of your work,
00:02:01.983 --> 00:02:04.900
maybe you're trying out some change
that just didn't work out,
00:02:04.900 --> 00:02:08.331
you can use the git reset --hard
to throw those commits away completely.
00:02:08.606 --> 00:02:11.268
People might ask, then why
would you make a commit?
00:02:11.268 --> 00:02:13.971
But Git provides you
a really nice safety net
00:02:13.971 --> 00:02:17.078
through the reflog command
that we'll look at in a future episode
00:02:17.078 --> 00:02:20.108
that motivates you to make
checkpoints with commits,
00:02:20.108 --> 00:02:22.353
and knowing that you have
hard at your disposal,
00:02:22.353 --> 00:02:24.560
you can clean that history up
anytime you like.
00:02:25.470 --> 00:02:27.084
♪ (music) ♪
00:02:27.760 --> 00:02:32.178
There is one other cousin command
that is often talked about with reset
00:02:32.178 --> 00:02:34.152
and that's the checkout command.
00:02:34.152 --> 00:02:37.702
The checkout command
is of a different granularity than reset,
00:02:37.702 --> 00:02:41.266
which typically operates
on the entire history of a repository.
00:02:41.703 --> 00:02:45.395
Checkout is focused more
on a directory or a file level precision.
00:02:45.756 --> 00:02:48.947
Instead of undoing or changing
an entire commit,
00:02:48.947 --> 00:02:52.710
we can go back in history
to a specific file at a commit
00:02:52.710 --> 00:02:57.297
and pull that file and its version
back to our current working directory.
00:02:57.685 --> 00:03:01.234
Clear communication is the goal
of all Git repository history.
00:03:01.487 --> 00:03:05.662
So whether it's git reset hard,
reset soft, reset mixed,
00:03:05.662 --> 00:03:08.145
or the more precise checkout command,
00:03:08.145 --> 00:03:12.757
use these to provide clear intent
about your changes to your colleagues.
00:03:12.954 --> 00:03:15.976
Git reset is a tool that can be
a little bit intimidating
00:03:15.976 --> 00:03:17.235
to new users of Git.
00:03:17.434 --> 00:03:20.184
But it can be a powerful tool
to have at your disposal
00:03:20.184 --> 00:03:22.473
for crafting beautiful repository history.
00:03:24.371 --> 00:03:28.029
Thanks for watching another episode
of Git and GitHub Foundations on reset.
00:03:28.029 --> 00:03:31.687
As always, don't forget to subscribe
to any of our channels on the side here.
00:03:31.687 --> 00:03:33.811
Questions or comments,
put those down below
00:03:33.811 --> 00:03:36.401
or check out one
of our other educational topics
00:03:36.401 --> 00:03:38.523
on Git and GitHub
here at the bottom.
00:03:38.789 --> 00:03:41.528
♪ (music) ♪
(voices silenced)