1 00:00:00,090 --> 00:00:03,840 In this video, we'll quickly talk about why you might want to rebase. 2 00:00:03,870 --> 00:00:09,630 We've kind of already touched on it, but also most importantly, when you should not rebase what the 3 00:00:09,630 --> 00:00:11,310 golden rule is of rebasing. 4 00:00:11,670 --> 00:00:13,710 So why rebase instead of merge? 5 00:00:14,040 --> 00:00:16,110 Imagine this repo is way more cluttered. 6 00:00:16,110 --> 00:00:18,690 Well, not cluttered, but there's a lot more history. 7 00:00:18,780 --> 00:00:23,400 Hundreds of commits and a lot of simultaneous work going on. 8 00:00:23,580 --> 00:00:30,000 We can see with the way of structure this very clearly what work I did on the feature branch. 9 00:00:30,060 --> 00:00:33,120 It's these three commits right here because we rebased. 10 00:00:33,120 --> 00:00:39,210 So it doesn't matter if a whole bunch of let's say there's ten or 50 or 100 other commits from the master 11 00:00:39,210 --> 00:00:44,430 branch and they happen simultaneously over the course of development that I was working on feature. 12 00:00:44,580 --> 00:00:49,790 Assuming that I get those changes, I pull them down to master and I rebase onto the master branch. 13 00:00:49,800 --> 00:00:53,640 My feature branch commits are all located together at the tip. 14 00:00:53,670 --> 00:00:59,670 This makes it easier to for someone to review my commits and take a look at the code base and try and 15 00:00:59,670 --> 00:01:04,830 read what's going on and open source projects with thousands of contributors and tons and tons of work 16 00:01:04,830 --> 00:01:05,620 all the time. 17 00:01:05,640 --> 00:01:09,240 This makes it easier for some random person to go read the history. 18 00:01:09,240 --> 00:01:11,820 Go try and understand what to this feature do. 19 00:01:11,850 --> 00:01:12,270 Right. 20 00:01:12,270 --> 00:01:16,250 I don't have to see like a million merge commits as part of the commit history. 21 00:01:16,260 --> 00:01:18,030 The end result is the same. 22 00:01:18,060 --> 00:01:24,420 I guess I didn't really actually show you the code here, but we still end up with the code coming from 23 00:01:24,420 --> 00:01:26,760 the master branch mixed in with a feature branch. 24 00:01:26,940 --> 00:01:28,650 So we have Image Gallery added. 25 00:01:28,650 --> 00:01:31,500 It's just the actual history that is updated. 26 00:01:32,150 --> 00:01:38,830 So because we are updating the history because rebase rewrites, commits, there is a big no, no, 27 00:01:38,840 --> 00:01:40,400 a big thing you should be aware of. 28 00:01:40,430 --> 00:01:44,780 You do not ever want to rebase commits that other people already have. 29 00:01:45,350 --> 00:01:51,380 So you don't want to rebase if you've pushed up some branch to GitHub or somewhere else. 30 00:01:51,380 --> 00:01:57,500 And your collaborators have that work on their machine because then they have those commits and then 31 00:01:57,500 --> 00:02:01,430 all of a sudden you rebase and you rewrite those commits, you rewrite history. 32 00:02:01,460 --> 00:02:03,050 That is a nightmare. 33 00:02:03,170 --> 00:02:06,590 It's not a nightmare, but it can be really, really annoying to reconcile. 34 00:02:06,920 --> 00:02:11,210 When your collaborators have different histories, you have commits that just don't even exist in their 35 00:02:11,210 --> 00:02:14,030 history, and they have commits that don't exist in yours. 36 00:02:14,030 --> 00:02:15,510 And it's all in the past. 37 00:02:15,530 --> 00:02:17,330 That can be very tricky. 38 00:02:17,360 --> 00:02:22,130 So again, the rule never rebase commits that you've shared with others. 39 00:02:22,370 --> 00:02:27,710 So what this means is you want to be rebasing commits that you have on your machine and other people 40 00:02:27,710 --> 00:02:28,250 don't. 41 00:02:28,250 --> 00:02:33,920 So your feature branches that you're working on, you don't want to rebase the master branch because 42 00:02:33,920 --> 00:02:38,910 people presumably have that master branch or the main branch they have commits that you have. 43 00:02:38,930 --> 00:02:45,540 So if you rewrite those commits and they're totally new, that's a problem when you try and push up. 44 00:02:45,560 --> 00:02:47,110 So you don't want to do that. 45 00:02:47,120 --> 00:02:49,660 At least that's a general principle. 46 00:02:49,670 --> 00:02:54,050 So you work on a feature branch, whatever you're doing in there, fixing a bug, adding a new feature, 47 00:02:54,050 --> 00:03:00,560 whatever it is, you can rebase your work from that branch onto the master branch or onto another branch. 48 00:03:00,590 --> 00:03:00,950 Right. 49 00:03:00,950 --> 00:03:02,870 You're not changing the master branch. 50 00:03:02,870 --> 00:03:06,230 You don't change the branch that you are rebasing onto. 51 00:03:06,310 --> 00:03:06,550 Right. 52 00:03:06,560 --> 00:03:08,690 We didn't change the master branch here. 53 00:03:09,080 --> 00:03:11,900 We only changed my commits on the feature branch. 54 00:03:12,110 --> 00:03:13,550 So that's important. 55 00:03:13,640 --> 00:03:20,120 The branch that you are on when you call rebase is the one that is rebased, as we talked about. 56 00:03:20,150 --> 00:03:21,140 Let's go back here. 57 00:03:22,410 --> 00:03:22,830 Here. 58 00:03:22,830 --> 00:03:26,080 I'm on the feature branch I rebase onto master. 59 00:03:26,100 --> 00:03:26,900 That's fine. 60 00:03:26,910 --> 00:03:28,350 I'm not messing up, master. 61 00:03:28,350 --> 00:03:30,320 I'm not changing the history on Master. 62 00:03:30,330 --> 00:03:32,910 I'm only changing the history on my feature branch. 63 00:03:32,910 --> 00:03:34,380 I'm changing those commits. 64 00:03:34,380 --> 00:03:35,550 So that's okay. 65 00:03:35,550 --> 00:03:40,020 You just want to avoid rebasing, commits, rewriting history that other people have. 66 00:03:40,020 --> 00:03:44,910 So once you've pushed that feature branch up, if somebody has pulled that down, if they have that 67 00:03:44,910 --> 00:03:49,680 history, if they're working on it, if you don't want to, you don't want to rebase, at least that's 68 00:03:49,680 --> 00:03:51,510 a general rule I recommend. 69 00:03:51,840 --> 00:03:55,680 Again, there are ways of fixing this, but it's not it's not fun. 70 00:03:55,680 --> 00:03:56,250 All right. 71 00:03:56,250 --> 00:04:02,850 So that is kind of why a lot of people shy away from rebase or why I was told not to use it, because 72 00:04:02,850 --> 00:04:05,880 you can get yourself in situations that are that are hairy. 73 00:04:05,880 --> 00:04:08,520 If you're collaborating with people, you're rewriting history. 74 00:04:08,520 --> 00:04:12,780 And if you don't know how to how to properly use rebase, that can be a big problem. 75 00:04:12,930 --> 00:04:15,120 So make sure you know what you're doing. 76 00:04:15,150 --> 00:04:18,839 And if you're working at a company like, they're going to make it very clear to you what the workflow 77 00:04:18,839 --> 00:04:19,050 is. 78 00:04:19,050 --> 00:04:25,470 And if they expect you to rebase everything or to never rebase and only merge or rebase in X situations, 79 00:04:25,470 --> 00:04:28,920 it's just part of learning the git workflow at a particular company. 80 00:04:28,920 --> 00:04:34,470 But on your own, if you're working on your own projects, just you, then you can rebase all the time, 81 00:04:34,470 --> 00:04:35,220 of course. 82 00:04:35,640 --> 00:04:41,880 But also, even if you're just on a small team rebase all the time, just make sure you know what your 83 00:04:41,880 --> 00:04:44,880 rebasing and don't rebase shared history any commits. 84 00:04:44,880 --> 00:04:49,230 People already have only rebase stuff that is yours and yours alone. 85 00:04:49,230 --> 00:04:51,720 Rebase it and then you can push it up. 86 00:04:51,720 --> 00:04:51,940 Right? 87 00:04:51,990 --> 00:04:57,150 You get this nice, clean history, you don't have all those merge commits, do that and then you can 88 00:04:57,150 --> 00:04:57,750 share that. 89 00:04:57,750 --> 00:05:02,280 So I'll show another example of this next with some conflicts if you want to see how that works. 90 00:05:02,280 --> 00:05:08,220 It's the same principle roughly as resolving merge conflicts when you're merging, but I'll demo it 91 00:05:08,220 --> 00:05:08,700 anyway.