1 00:00:00,090 --> 00:00:04,700 So as I mentioned, there are two main things that we can do with the get rebase command. 2 00:00:04,710 --> 00:00:10,140 We're going to focus on the first of those things, using it as an alternative to the git merge command. 3 00:00:10,140 --> 00:00:15,540 So both commands get merged, get rebates, help us combine changes from two branches. 4 00:00:16,500 --> 00:00:23,070 So I'm going to start by identifying the I guess you could call it the problem with merging or something 5 00:00:23,070 --> 00:00:25,950 that rebasing addresses that merging does not. 6 00:00:25,950 --> 00:00:32,430 So imagine that I'm working on a project with, let's say, ten different developers. 7 00:00:32,549 --> 00:00:37,050 I'm at work and we're working on some big code base together, ten of us. 8 00:00:37,170 --> 00:00:41,880 Okay, so we have our master branch and there's like a million commits before it that you can't see. 9 00:00:41,880 --> 00:00:46,560 Well, maybe a couple thousand commits then it's my turn. 10 00:00:46,560 --> 00:00:47,190 We have stand up. 11 00:00:47,190 --> 00:00:49,260 It's my turn to work on our new feature this morning. 12 00:00:49,260 --> 00:00:52,500 Well, everyone works in a new feature, but it's time for me to start a new feature. 13 00:00:52,500 --> 00:00:53,970 So I make a new feature branch. 14 00:00:53,970 --> 00:00:58,140 We'll just call it feature and I do some work on that feature branch. 15 00:00:58,140 --> 00:01:03,030 Right, so there's a new commit or two or maybe more continue doing more work. 16 00:01:03,030 --> 00:01:09,300 We end up with more commits on my branch, but in the meantime, maybe a couple of hours pass, maybe 17 00:01:09,300 --> 00:01:16,480 it's lunch and one of my other coworkers finish their their task, their bug fix or their new feature 18 00:01:16,480 --> 00:01:20,370 or whatever, and that code is merged into master. 19 00:01:20,370 --> 00:01:26,100 Now probably they make a pull request or there's some mechanism in between, but the end result is that 20 00:01:26,100 --> 00:01:28,650 their changes are merged into master. 21 00:01:29,130 --> 00:01:32,700 So I probably want that new work on my feature branch. 22 00:01:32,700 --> 00:01:33,030 Right. 23 00:01:33,030 --> 00:01:35,580 Maybe there's a bug fix in there or something. 24 00:01:35,580 --> 00:01:40,590 I don't want to diverge from the master branch for a super long time without getting those new changes. 25 00:01:40,590 --> 00:01:42,840 So how do I get that new commit? 26 00:01:42,840 --> 00:01:44,790 Hopefully this is nothing new. 27 00:01:44,820 --> 00:01:48,270 How do I get that new work from the master branch onto my feature branch? 28 00:01:48,360 --> 00:01:50,880 Well, I merge, right. 29 00:01:50,880 --> 00:01:53,520 So this cannot be performed as a fast forward merge. 30 00:01:53,520 --> 00:01:55,260 We end up with a merge commit. 31 00:01:55,260 --> 00:02:00,720 That's what I'm highlighting that purple background with a green or whatever cyan color that's supposed 32 00:02:00,720 --> 00:02:02,100 to indicate a merge commit. 33 00:02:02,640 --> 00:02:06,480 So there's a merge commit on my branch and then I keep doing some more work. 34 00:02:06,480 --> 00:02:07,200 Right. 35 00:02:07,650 --> 00:02:13,200 And then what do you know as I'm doing more work the next day on my feature branch, there's more new 36 00:02:13,200 --> 00:02:14,730 work added to the master branch. 37 00:02:14,730 --> 00:02:16,980 Another coworker pushes up some changes. 38 00:02:16,980 --> 00:02:18,780 Maybe it's even more than one. 39 00:02:18,780 --> 00:02:23,820 Maybe it happened five or six times in a day, depending on what they're fixing or working on. 40 00:02:23,820 --> 00:02:28,950 So I need to get those commits onto my feature branch or I need to know about them. 41 00:02:28,950 --> 00:02:30,270 So I do the same thing. 42 00:02:30,270 --> 00:02:35,070 I merge and yet again I have a new merge commit, right? 43 00:02:35,070 --> 00:02:41,400 So as I continue to work, imagine that instead of what five commits here or something, I have a whole 44 00:02:41,400 --> 00:02:43,890 bunch of commits on my branch, my feature branch. 45 00:02:43,890 --> 00:02:49,890 Maybe I'm working on it over the course of weeks, not just days, and the rest of the team is very 46 00:02:49,890 --> 00:02:50,310 active. 47 00:02:50,310 --> 00:02:54,180 So we have a very active master branch, lots of changes happening in the meantime. 48 00:02:54,180 --> 00:02:59,460 So I need to continuously merge those in and I end up with all these merge commits. 49 00:02:59,460 --> 00:03:06,030 So my entire feature branch might have dozens of merge commits that don't actually say anything about 50 00:03:06,030 --> 00:03:07,080 what I'm working on. 51 00:03:07,110 --> 00:03:10,710 They have nothing to do with my actual code or with my commits. 52 00:03:10,740 --> 00:03:13,610 They just have to be there because I merged, right? 53 00:03:13,620 --> 00:03:15,060 We don't have a way around that. 54 00:03:15,060 --> 00:03:18,720 So again, try and visualize this on a much larger scale. 55 00:03:18,720 --> 00:03:23,820 So instead of just the small number of commits, I can fit on this one slide and a tiny diagram. 56 00:03:23,940 --> 00:03:29,340 Imagine that I'm merging pretty frequently just to get those changes from master whatever my coworkers 57 00:03:29,340 --> 00:03:34,710 are doing, and if it's a very, very active project, that could be a lot of merging onto each feature 58 00:03:34,710 --> 00:03:35,220 branch. 59 00:03:35,220 --> 00:03:37,380 And my coworkers all have to do that too, right? 60 00:03:37,380 --> 00:03:38,430 I'm not the only one. 61 00:03:38,430 --> 00:03:43,830 They're all working on features and if everyone else has merged changes into Master, they need to get 62 00:03:43,830 --> 00:03:45,810 those changes onto their feature branches. 63 00:03:45,810 --> 00:03:51,930 So everyone's feature branches might be riddled with a bunch of merge commits and then when they merge 64 00:03:51,930 --> 00:03:57,060 those back into master, there's just a bunch of useless merge commits as part of the history. 65 00:03:57,090 --> 00:03:59,880 I shouldn't say useless, but they're not informative. 66 00:04:00,210 --> 00:04:08,340 The commits might be something like fix ex bug, add new feature, remove x file and then merge commit 67 00:04:08,520 --> 00:04:10,170 merge blob branch. 68 00:04:10,170 --> 00:04:10,440 Right. 69 00:04:10,440 --> 00:04:17,490 That's just sprinkled in there and that makes our history more annoying, muddier, less clean. 70 00:04:17,490 --> 00:04:20,339 So that is something that rebasing can help us with. 71 00:04:20,610 --> 00:04:23,040 So I'm going to show an example repo. 72 00:04:23,040 --> 00:04:29,670 We'll set it up in the next video, but I'll just show you on slides for now if we rebase instead. 73 00:04:29,670 --> 00:04:34,500 So rebasing is just another way of combining branches instead of merging. 74 00:04:34,710 --> 00:04:40,860 What happens when we rebase is that we actually rewrite history and this is why it's sometimes problematic. 75 00:04:40,860 --> 00:04:46,830 We are rewriting history, we're creating new commits, or we have git create new commits for us based 76 00:04:46,830 --> 00:04:49,590 upon the original feature branch commits. 77 00:04:49,830 --> 00:04:52,890 So here's a diagram, the same diagram from before up top. 78 00:04:52,890 --> 00:04:54,450 Here's my feature branch. 79 00:04:54,600 --> 00:04:59,880 This is what happens when I merge in all those changes right from the master branch and I get multiple. 80 00:04:59,970 --> 00:05:06,810 Merge commits, but if I use rebase so I'm going to rebase the feature branch onto the master branch. 81 00:05:06,840 --> 00:05:09,990 Think of the term rebase as re. 82 00:05:10,200 --> 00:05:12,420 Well that was I was going to say rebase. 83 00:05:12,690 --> 00:05:13,770 That's exactly what it is. 84 00:05:13,770 --> 00:05:17,980 We are coming up with a new base for our feature branch. 85 00:05:18,000 --> 00:05:21,100 So what we end up with is this linear structure. 86 00:05:21,120 --> 00:05:25,410 We will go into detail again on how this all works and I'll demo it. 87 00:05:25,410 --> 00:05:27,450 But notice a linear structure. 88 00:05:27,450 --> 00:05:33,480 We have the master branch and then we have our feature commits from that feature branch on top of the 89 00:05:33,480 --> 00:05:36,890 master branch added to the tip of that master branch. 90 00:05:36,900 --> 00:05:39,440 So we don't have these merge commits, right? 91 00:05:39,450 --> 00:05:43,890 We have the same original like the three commits I did on the feature branch, the four commits from 92 00:05:43,890 --> 00:05:47,010 the master branch, but they've actually been rewritten. 93 00:05:47,010 --> 00:05:51,670 So this doesn't indicate the time where things were made, where commits were created. 94 00:05:51,690 --> 00:05:58,200 Each commit actually has a date, so that is preserved, but commits are moved around in order to give 95 00:05:58,200 --> 00:05:59,460 us this new structure. 96 00:05:59,520 --> 00:06:05,040 So all that feature branch work I have done, all those commits begin at the tip of the master branch. 97 00:06:05,340 --> 00:06:10,830 So I don't have these merge commits anymore, but the end result is that whatever I'm working on here 98 00:06:10,830 --> 00:06:16,080 contains all of the commits from master and from feature, just like it does right here. 99 00:06:16,380 --> 00:06:18,180 It's just a different mechanism. 100 00:06:18,180 --> 00:06:23,460 And here's a similar example where instead of having these merge commits, I just wait. 101 00:06:23,460 --> 00:06:27,630 So I have my feature branch and then I want to get the changes from Master. 102 00:06:27,660 --> 00:06:28,980 Combine them with feature. 103 00:06:28,980 --> 00:06:33,350 Those new commits I can rebase in order to end up with this structure. 104 00:06:33,360 --> 00:06:37,710 My feature branch has a new base at the tip of the master branch. 105 00:06:37,710 --> 00:06:44,520 So it begins where master ends and we end up with this nice structure, linear, it's cleaner and it's 106 00:06:44,520 --> 00:06:45,480 more focused. 107 00:06:45,480 --> 00:06:46,710 Write the history here. 108 00:06:46,710 --> 00:06:48,240 Someone can see very quickly. 109 00:06:48,240 --> 00:06:53,580 These are the commits that had to do with my branch and the changes I made versus here. 110 00:06:53,610 --> 00:06:57,410 Those commits are still there, but there's these merge commits that are in the way. 111 00:06:57,420 --> 00:07:02,430 So if we multiply that by ten people working on feature branches merging all the time, that's a lot 112 00:07:02,430 --> 00:07:07,410 of merge commits, but if they all rebase in the correct way, it's a lot easier to see what's going 113 00:07:07,410 --> 00:07:09,570 on with their individual branch history. 114 00:07:10,020 --> 00:07:10,320 All right. 115 00:07:10,320 --> 00:07:12,690 So I'm going to demo this in the next couple of videos.