1 00:00:00,240 --> 00:00:03,510 All right, so we finally get to try the rebase command. 2 00:00:03,810 --> 00:00:06,570 I'm going to do exactly what I'm showing you in this diagram. 3 00:00:06,600 --> 00:00:15,060 Currently, I have my FT branch feature, whatever that feature is, where I have twice merged in changes 4 00:00:15,060 --> 00:00:15,910 from master. 5 00:00:15,930 --> 00:00:18,810 So my master branch still doesn't have any of the feature stuff. 6 00:00:18,810 --> 00:00:19,510 That's fine. 7 00:00:19,530 --> 00:00:22,330 I don't want to directly merge anything into master right now. 8 00:00:22,350 --> 00:00:27,750 Instead, I'm just concerned about getting the changes from my coworkers that are constantly adding 9 00:00:27,780 --> 00:00:29,960 to master, even if it's not constant. 10 00:00:29,970 --> 00:00:35,550 Let's just say frequently in a given week, updating master pole requests are merged in I need that 11 00:00:35,550 --> 00:00:41,160 work on my feature branch, so I merge from master into feature, but that gives me merge commits. 12 00:00:41,430 --> 00:00:44,460 So instead I'm going to rebase. 13 00:00:44,460 --> 00:00:49,260 And again when we use the git rebase command, we are rewriting history. 14 00:00:49,890 --> 00:00:53,280 So this is the actual history of our commits. 15 00:00:53,730 --> 00:00:57,480 But when I rebase, things get switched around a bit. 16 00:00:57,480 --> 00:01:00,030 In fact, we get new commits generated. 17 00:01:00,060 --> 00:01:05,370 So instead of creating merge commits for us when we use git rebase, it rewrites history. 18 00:01:05,370 --> 00:01:09,510 It creates brand new commits for each of the original feature branch commits. 19 00:01:09,660 --> 00:01:12,020 So the workflow here looks like this. 20 00:01:12,030 --> 00:01:18,390 I want to be on my feature branch and then I call or I run git rebase master. 21 00:01:19,050 --> 00:01:23,430 So this is not going to screw up or change the master branch. 22 00:01:23,460 --> 00:01:29,970 Instead, it's going to rewrite history by taking my feature branch commits, creating new ones that 23 00:01:29,970 --> 00:01:35,460 are based on the originals and putting them all at the tip of the master branch or starting them at 24 00:01:35,460 --> 00:01:36,720 the tip of the master branch. 25 00:01:36,750 --> 00:01:40,680 Re Basing them at the tip of this master. 26 00:01:40,950 --> 00:01:46,560 So all that work, however many commits I have on my feature branch, that work is not going to be destroyed 27 00:01:46,560 --> 00:01:52,440 or anything like that, but the commits are going to be recreated and they're going to be added to the 28 00:01:52,440 --> 00:01:53,620 tip of my master branch. 29 00:01:53,640 --> 00:01:55,980 So we end up with a linear structure. 30 00:01:55,980 --> 00:02:00,630 We still have two branches, of course, because a branch is just a pointer with just a reference. 31 00:02:00,630 --> 00:02:05,520 But now we don't have these merge commits where if we don't really need to go into it now, but each 32 00:02:05,520 --> 00:02:09,289 one of these merge commits has two parents here. 33 00:02:09,300 --> 00:02:11,520 Every commit has a single parent. 34 00:02:11,730 --> 00:02:14,280 Except I guess the first commit on the master branch. 35 00:02:14,580 --> 00:02:16,150 So let me demonstrate this. 36 00:02:16,170 --> 00:02:20,640 As we saw, we already have merge commits so we can rebase now. 37 00:02:21,330 --> 00:02:27,100 I'm showing you sort of merging first and then rebasing, but you can actually rebase instead of merging. 38 00:02:27,120 --> 00:02:28,130 I'll show that. 39 00:02:28,140 --> 00:02:30,030 So here's our structure right now. 40 00:02:30,300 --> 00:02:33,030 I'll zoom in as much as I can in post-production. 41 00:02:33,030 --> 00:02:38,610 So we have Master Branch here on the right feature here and we have these merge commits. 42 00:02:38,910 --> 00:02:43,080 So instead of this structure, I'm going to rebase. 43 00:02:43,080 --> 00:02:46,800 And I guess what I'll start with is I'll show you these commit hashes. 44 00:02:46,800 --> 00:02:48,600 Maybe I'll take a screenshot of them. 45 00:02:49,650 --> 00:02:51,390 Now I'm going to run this command. 46 00:02:51,390 --> 00:02:53,010 So I'm on the feature branch. 47 00:02:53,490 --> 00:02:56,880 Let's say I make sure I get the latest changes on the master branch. 48 00:02:56,880 --> 00:02:58,740 I pull down from GitHub on master. 49 00:02:58,770 --> 00:03:02,280 I've got the latest that whatever coworkers have merged their code in. 50 00:03:02,310 --> 00:03:04,800 Now I'm going to call get rebase. 51 00:03:06,410 --> 00:03:08,030 I can type it, master. 52 00:03:08,030 --> 00:03:10,910 And I'm calling this from my feature branch. 53 00:03:11,690 --> 00:03:12,320 Okay. 54 00:03:12,320 --> 00:03:19,370 So take a look at this output rewinding head to replay your work on top of it so on top of master and 55 00:03:19,370 --> 00:03:22,100 then it applies my commits, my three commits. 56 00:03:22,100 --> 00:03:23,870 So let's see what that looks like. 57 00:03:24,290 --> 00:03:25,760 So I'll zoom in here. 58 00:03:26,240 --> 00:03:30,020 Hopefully I do follow through and zoom in in post-production. 59 00:03:30,110 --> 00:03:33,590 And our structure has changed pretty radically. 60 00:03:34,040 --> 00:03:35,840 So all of my work is still here. 61 00:03:36,020 --> 00:03:38,420 We still have those three commits I made on feature. 62 00:03:38,420 --> 00:03:40,520 We just don't have those two merge commits. 63 00:03:40,520 --> 00:03:42,440 So now things are linear. 64 00:03:42,440 --> 00:03:47,840 We still have two branches of course, but we've created new commits or git has created new commits 65 00:03:47,840 --> 00:03:51,470 for us based upon the original commits on the feature branch. 66 00:03:51,710 --> 00:03:54,770 So if I type get logged now first I'll just show you. 67 00:03:54,770 --> 00:04:00,740 Still two branches master is unchanged, but now if I type get log I'll do one line. 68 00:04:01,820 --> 00:04:07,820 We have fewer commits because we don't have those merge commits and the commit hashes have changed because 69 00:04:07,820 --> 00:04:09,800 we actually end up with new commits. 70 00:04:09,800 --> 00:04:17,480 So the original commits on the master branch are unchanged to be to be three for f, three for f. 71 00:04:17,480 --> 00:04:18,079 Right. 72 00:04:18,110 --> 00:04:22,520 What else is on master add footer e19e19. 73 00:04:22,730 --> 00:04:29,600 Anyway, the three commits that I made on the feature branch, however, like begin feature if we take 74 00:04:29,600 --> 00:04:33,050 a look at it on the original screenshot, I took eight, eight, nine. 75 00:04:33,050 --> 00:04:34,190 Very different. 76 00:04:34,220 --> 00:04:42,140 It's a completely new commit 907 So I rewrote history, I made new commits, the work is still there. 77 00:04:42,350 --> 00:04:47,510 I still have those commits, the commit messages and actually the dates are still in there, the metadata, 78 00:04:47,510 --> 00:04:49,550 but they are in fact new commits. 79 00:04:49,550 --> 00:04:53,090 It's not just like an update to a committed, it's new history. 80 00:04:53,090 --> 00:04:59,660 So what this does, though, is hopefully you can see it cleans up my history for this feature branch 81 00:04:59,660 --> 00:05:05,960 significantly and it makes it easier to see what I've done, like the actual work on this branch because 82 00:05:05,960 --> 00:05:07,370 it's all here now. 83 00:05:07,640 --> 00:05:13,520 It's not mixed in with that work from master, even though the code is all there together, but the 84 00:05:13,520 --> 00:05:15,110 commits are all together. 85 00:05:15,110 --> 00:05:16,310 They're condensed down. 86 00:05:16,310 --> 00:05:18,700 So I can see, Oh, this is what I've done, right? 87 00:05:18,710 --> 00:05:23,030 Those are the commits I've made on this branch versus originally. 88 00:05:23,030 --> 00:05:24,650 Let me pull that screenshot up again. 89 00:05:24,650 --> 00:05:30,890 If we look here, we've got this from the master branch and then this in the Master Branch Feature Branch 90 00:05:30,890 --> 00:05:32,750 Feature Master. 91 00:05:32,780 --> 00:05:39,680 This is on feature, it's a merge commit feature master feature versus here it's just master master, 92 00:05:39,680 --> 00:05:44,180 master, master those four and then feature feature feature. 93 00:05:45,020 --> 00:05:49,910 Master, branch, master branch, master branch, master branch and then feature, feature, feature. 94 00:05:51,110 --> 00:05:54,440 So that is our first example of using Rebase. 95 00:05:54,950 --> 00:05:59,840 We've done exactly what these diagrams showed, but I also want to show that we can just use rebase 96 00:05:59,840 --> 00:06:01,080 instead of merging. 97 00:06:01,100 --> 00:06:05,480 For example, if we quickly make another change on the master branch. 98 00:06:07,350 --> 00:06:08,580 So we're on the master branch. 99 00:06:08,580 --> 00:06:10,440 What else is added login form? 100 00:06:10,620 --> 00:06:12,930 How about image gallery added? 101 00:06:12,930 --> 00:06:14,010 I don't know really. 102 00:06:14,730 --> 00:06:19,170 This is probably not how you would break up development, but anyway, let's just do something obvious 103 00:06:19,170 --> 00:06:19,470 like that. 104 00:06:19,470 --> 00:06:20,670 Image Gallery added. 105 00:06:21,330 --> 00:06:26,580 So I'm going to do a commit on the master branch add image gallery. 106 00:06:27,660 --> 00:06:29,100 So that's something a coworker does. 107 00:06:29,100 --> 00:06:30,540 I switch back to feature. 108 00:06:30,810 --> 00:06:32,100 I don't have that right. 109 00:06:32,100 --> 00:06:33,810 So now we've diverged again. 110 00:06:33,810 --> 00:06:39,240 We don't have this linear structure and if I want that work that I just got pulled down from GitHub, 111 00:06:39,240 --> 00:06:40,740 let's say that a coworker did. 112 00:06:40,740 --> 00:06:43,830 I want that on my feature branch that I'm currently developing. 113 00:06:43,830 --> 00:06:46,590 I want to stay up to date instead of merging. 114 00:06:46,710 --> 00:06:52,470 I'm now going to rebase and the end result will be what I just showed you in the previous example, 115 00:06:52,470 --> 00:06:55,770 where we've rebase the feature branch on top of master. 116 00:06:55,770 --> 00:07:01,110 So instead of merging and having a merge commit here on the feature branch are three feature branch 117 00:07:01,110 --> 00:07:05,250 commits are just moved on top of that last master branch commit. 118 00:07:05,250 --> 00:07:08,280 So we rebase it on the tip of the master branch. 119 00:07:09,130 --> 00:07:09,640 Okay. 120 00:07:09,670 --> 00:07:14,630 So I don't have this commit on my feature branch about what was it again. 121 00:07:14,650 --> 00:07:15,580 Image Gallery. 122 00:07:15,580 --> 00:07:19,210 I don't have that on feature branch, but I'm going to switch to it. 123 00:07:19,210 --> 00:07:19,990 I'm currently on it. 124 00:07:19,990 --> 00:07:22,480 So make sure if that we're on feature. 125 00:07:22,510 --> 00:07:25,600 Oh and if I want just to check in, what's the difference. 126 00:07:25,810 --> 00:07:34,420 I can do get diff feet master and quickly see I'm don't have this image gallery added I mean we know 127 00:07:34,420 --> 00:07:40,150 that but if there was more work I would see there's a bigger diff between feature and master or vice 128 00:07:40,150 --> 00:07:40,720 versa. 129 00:07:40,720 --> 00:07:43,510 So I'm going to rebase get rebase master. 130 00:07:43,510 --> 00:07:45,100 I'm on the feature branch. 131 00:07:46,300 --> 00:07:46,930 Okay. 132 00:07:46,930 --> 00:07:49,420 So this is the current structure, what we see. 133 00:07:50,310 --> 00:07:57,750 And get re master here we go rewinds head to replay our work and this is what we end up with. 134 00:07:57,960 --> 00:08:01,630 So even though there was just one new commit on master, right? 135 00:08:01,710 --> 00:08:06,750 This ad image gallery commit all of my commits on feature were recreated. 136 00:08:06,750 --> 00:08:13,170 There are new commits and I didn't take a screenshot of the commit hashes, but these are different 137 00:08:13,170 --> 00:08:18,150 from what they were just a moment ago because these are new commits based on the original commits. 138 00:08:18,480 --> 00:08:21,450 And so now we're back to this linear structure, right? 139 00:08:21,720 --> 00:08:23,580 So I can do this instead of merging. 140 00:08:23,580 --> 00:08:24,870 I don't need to merge. 141 00:08:24,870 --> 00:08:30,300 I can only rebase or I guess as I showed you originally, you can merge, merge, merge. 142 00:08:30,300 --> 00:08:35,100 And then at the end say, okay, I'm going to rebase now before I share this with people, I don't want 143 00:08:35,100 --> 00:08:36,809 them to see all my merge commits. 144 00:08:37,710 --> 00:08:42,720 It's really depends on the workflow and we'll talk more about the pros and cons and especially about 145 00:08:42,720 --> 00:08:46,920 the golden rule of rebasing, which is when not to rebase. 146 00:08:47,520 --> 00:08:53,760 So hopefully this is becoming a little bit clearer when we rebase a branch, we are basing that branch 147 00:08:53,760 --> 00:08:55,950 on the tip of another branch. 148 00:08:56,100 --> 00:09:02,180 So if we switch to whatever branch our feature branch, we rebase on to master. 149 00:09:02,190 --> 00:09:05,130 That's the part that sometimes a see students get confused about. 150 00:09:05,130 --> 00:09:11,220 They think get rebase master means you're actually rebasing the master branch, but in fact we are rebasing 151 00:09:11,220 --> 00:09:13,380 on to master in this case. 152 00:09:13,590 --> 00:09:20,430 So our feature branch commits are recreated, new history is made and it all begins at the tip of the 153 00:09:20,430 --> 00:09:23,460 master branch and we end up with this nice linear structure.