1 00:00:00,206 --> 00:00:04,616 Git reset is the command that allows you to shape history in your repository. 2 00:00:04,616 --> 00:00:07,945 Whether you're undoing some changes or crafting your commits differently, 3 00:00:07,945 --> 00:00:09,401 this is the command for you. 4 00:00:09,651 --> 00:00:11,241 ♪ (bossa music) ♪ 5 00:00:14,751 --> 00:00:18,153 Welcome to the episode of Git and GitHub Foundations on the reset command. 6 00:00:18,538 --> 00:00:21,560 This command has a lot of power, and a lot of modes. 7 00:00:21,560 --> 00:00:24,512 Yeah, there's going to be three modes we're going to be working with here. 8 00:00:24,512 --> 00:00:28,236 There's going to be soft; mixed, or the default; and hard. 9 00:00:28,555 --> 00:00:30,587 ♪ (music) ♪ 10 00:00:31,032 --> 00:00:32,921 these adjectives are pretty descriptive, 11 00:00:33,088 --> 00:00:35,513 starting with the one that's the default, mixed. 12 00:00:35,830 --> 00:00:39,648 It alters both the history and the working directory. 13 00:00:39,998 --> 00:00:41,708 So mixed, both things. 14 00:00:41,898 --> 00:00:44,427 And soft is going to take one or multiple commits 15 00:00:44,427 --> 00:00:47,179 and put all of their changes back into the staging area 16 00:00:47,179 --> 00:00:50,048 to allow you to continue to craft a new commit out of that. 17 00:00:50,433 --> 00:00:53,279 Hard, on the other hand, is a destructive operation 18 00:00:53,279 --> 00:00:55,893 that's meant to erase things that you no longer wish to keep. 19 00:00:56,212 --> 00:00:59,552 So with all three of these in our stable, 20 00:00:59,552 --> 00:01:01,430 let's look at mixed first. 21 00:01:03,475 --> 00:01:05,173 Mixed is the recommendation 22 00:01:05,173 --> 00:01:07,242 that gets most often shown to new Git users 23 00:01:07,271 --> 00:01:09,444 because it shows up in the status command. 24 00:01:09,609 --> 00:01:12,391 When you have changes in the staging area and you say git status, 25 00:01:12,621 --> 00:01:14,493 we see a Git reset head. 26 00:01:14,493 --> 00:01:17,046 That allows us to take those changes out of the staging area 27 00:01:17,046 --> 00:01:18,875 and put them back into the working directory 28 00:01:18,875 --> 00:01:20,371 with the mixed option. 29 00:01:20,672 --> 00:01:23,379 Soft is the way that I take some changes 30 00:01:23,379 --> 00:01:25,706 that might have gotten too granular in nature. 31 00:01:26,033 --> 00:01:27,869 One, two, three, four, five commits 32 00:01:27,869 --> 00:01:30,846 that I then realize all belong in one transaction, 33 00:01:30,846 --> 00:01:36,907 and to bring them all together by a git reset --soft HEAD~5 34 00:01:36,907 --> 00:01:38,610 the most recent five commits. 35 00:01:38,835 --> 00:01:41,100 So with that command of using soft 36 00:01:41,100 --> 00:01:42,696 you can take all five of those commits 37 00:01:42,696 --> 00:01:46,285 and kind of squash them down into the staging area as one commit. 38 00:01:46,665 --> 00:01:48,765 Pretty useful for reshaping history, 39 00:01:48,765 --> 00:01:51,295 but reshaping history goes to the far end of the spectrum 40 00:01:51,295 --> 00:01:54,630 to throw away commits that just don't add value. 41 00:01:54,877 --> 00:01:57,748 You don't want to send them, transmit them, or share them with others. 42 00:01:57,922 --> 00:01:59,521 That's where hard comes into play. 43 00:01:59,521 --> 00:02:01,853 If you completely want to throw away some of your work, 44 00:02:01,853 --> 00:02:04,710 maybe you're trying out some change that just didn't work out, 45 00:02:04,710 --> 00:02:08,061 you can use the git reset --hard to throw those commits away completely. 46 00:02:08,606 --> 00:02:11,268 People might ask, then why would you make a commit? 47 00:02:11,268 --> 00:02:13,971 But Git provides you a really nice safety net 48 00:02:13,971 --> 00:02:17,078 through the reflog command that we'll look at in a future episode 49 00:02:17,078 --> 00:02:20,488 that motivates you to make checkpoints with commits, 50 00:02:20,488 --> 00:02:22,653 and knowing that you have hard at your disposal, 51 00:02:22,653 --> 00:02:25,180 you can clean that history up anytime you like. 52 00:02:25,850 --> 00:02:27,464 ♪ (music) ♪ 53 00:02:27,760 --> 00:02:32,178 There is one other cousin command that is often talked about with reset 54 00:02:32,178 --> 00:02:34,152 and that's the checkout command. 55 00:02:34,152 --> 00:02:37,702 The checkout command is of a different granularity than reset, 56 00:02:37,702 --> 00:02:41,266 which typically operates on the entire history of a repository. 57 00:02:41,703 --> 00:02:45,395 Checkout is focused more on a directory or a file level precision. 58 00:02:45,756 --> 00:02:49,287 Instead of undoing or changing an entire commit, 59 00:02:49,287 --> 00:02:52,710 we can go back in history to a specific file at a commit 60 00:02:52,710 --> 00:02:57,297 and pull that file and its version back to our current working directory. 61 00:02:57,685 --> 00:03:01,234 Clear communication is the goal of all Git repository history. 62 00:03:01,487 --> 00:03:06,022 So whether it's git reset --hard, reset --soft, reset --mixed, 63 00:03:06,022 --> 00:03:08,145 or the more precise checkout command, 64 00:03:08,145 --> 00:03:12,837 use these to provide clear intent about your changes to your colleagues. 65 00:03:13,284 --> 00:03:15,976 Git reset is a tool that can be a little bit intimidating 66 00:03:15,976 --> 00:03:17,235 to new users of Git. 67 00:03:17,434 --> 00:03:20,184 But it can be a powerful tool to have at your disposal 68 00:03:20,184 --> 00:03:22,473 for crafting beautiful repository history. 69 00:03:24,181 --> 00:03:28,029 Thanks for watching another episode of Git and GitHub Foundations on reset. 70 00:03:28,029 --> 00:03:31,687 As always, don't forget to subscribe to any of our channels on the side her. 71 00:03:31,687 --> 00:03:33,811 Questions or comments? Put those down below 72 00:03:33,811 --> 00:03:36,401 or check out one of our other educational topics 73 00:03:36,401 --> 00:03:38,523 on Git and GitHub here at the bottom. 74 00:03:38,789 --> 00:03:40,658 ♪ (music) ♪