1 99:59:59,999 --> 99:59:59,999 Git Rebase is the ability to take existing commits, 2 99:59:59,999 --> 99:59:59,999 and to place them on a branch that starts today. 3 99:59:59,999 --> 99:59:59,999 ♪ (jazzy theme music throughout) ♪ 4 99:59:59,999 --> 99:59:59,999 Creating a branch is a tough decision. 5 99:59:59,999 --> 99:59:59,999 Start it today, or start it later? 6 99:59:59,999 --> 99:59:59,999 Well, some important fixes might be happening right now. 7 99:59:59,999 --> 99:59:59,999 So better wait until tomorrow. 8 99:59:59,999 --> 99:59:59,999 That difficult decision has gone away with Git. 9 99:59:59,999 --> 99:59:59,999 Start a branch whenever you like, 10 99:59:59,999 --> 99:59:59,999 and make it contain the changes that you intend to deliver. 11 99:59:59,999 --> 99:59:59,999 Focused on a particular feature, 12 99:59:59,999 --> 99:59:59,999 bug fix, or objective. 13 99:59:59,999 --> 99:59:59,999 What about wanting this to start 14 99:59:59,999 --> 99:59:59,999 at a later place in history, 15 99:59:59,999 --> 99:59:59,999 in order to incorporate those hot fixes 16 99:59:59,999 --> 99:59:59,999 that are going on to the master branch? 17 99:59:59,999 --> 99:59:59,999 No problem. Rebase to the rescue. 18 99:59:59,999 --> 99:59:59,999 Rebase, in its standard usage form, 19 99:59:59,999 --> 99:59:59,999 allows one branch to be relocated farther down the history track, 20 99:59:59,999 --> 99:59:59,999 meaning taking all of the changes that are isolated on your branch, 21 99:59:59,999 --> 99:59:59,999 and acting as if they happened after 22 99:59:59,999 --> 99:59:59,999 all of the modern work on the master branch. 23 99:59:59,999 --> 99:59:59,999 This accomplishes the same thing, but with cleaner history, 24 99:59:59,999 --> 99:59:59,999 than doing a reverse merge 25 99:59:59,999 --> 99:59:59,999 from the master branch to the feature branch. 26 99:59:59,999 --> 99:59:59,999 The same contents will be present in the feature branch, 27 99:59:59,999 --> 99:59:59,999 but without the complication of the merge, 28 99:59:59,999 --> 99:59:59,999 going into that feature branch, and recorded in its history. 29 99:59:59,999 --> 99:59:59,999 It's important to note that the rebase command 30 99:59:59,999 --> 99:59:59,999 is altering all of the commits present in the branch. 31 99:59:59,999 --> 99:59:59,999 It's preserving all of the work you did, 32 99:59:59,999 --> 99:59:59,999 but their location and relationship to other commits 33 99:59:59,999 --> 99:59:59,999 is all changing. 34 99:59:59,999 --> 99:59:59,999 This is primarily used for a branch that you only own, 35 99:59:59,999 --> 99:59:59,999 and isn't being worked on by others. 36 99:59:59,999 --> 99:59:59,999 The change in the relationship, 37 99:59:59,999 --> 99:59:59,999 and identifier for each of those commits, 38 99:59:59,999 --> 99:59:59,999 makes it difficult to reconcile with the work of others. 39 99:59:59,999 --> 99:59:59,999 We're primarily talking about a branch that you are focused on. 40 99:59:59,999 --> 99:59:59,999 But with those constraints in place, the use is quite simple. 41 99:59:59,999 --> 99:59:59,999 git checkout to the feature branch, 42 99:59:59,999 --> 99:59:59,999 git rebase on the source branch, typically master. 43 99:59:59,999 --> 99:59:59,999 That will then iteratively walk through all of the commits 44 99:59:59,999 --> 99:59:59,999 that have happened on the feature branch, 45 99:59:59,999 --> 99:59:59,999 and replay them as if they were being robotically rewritten, 46 99:59:59,999 --> 99:59:59,999 starting from the latest point in time on the master branch. 47 99:59:59,999 --> 99:59:59,999 When the process completes, 48 99:59:59,999 --> 99:59:59,999 after seeing it step through all of the individual commits, 49 99:59:59,999 --> 99:59:59,999 it will let you know that the rebase is complete, 50 99:59:59,999 --> 99:59:59,999 and you'll return to the command prompt 51 99:59:59,999 --> 99:59:59,999 and what appears to be a similar state 52 99:59:59,999 --> 99:59:59,999 to before you ran that instruction. 53 99:59:59,999 --> 99:59:59,999 However, all of those historical commits now have new identifiers, 54 99:59:59,999 --> 99:59:59,999 keep that in mind. 55 99:59:59,999 --> 99:59:59,999 You'll find a request to use this pattern 56 99:59:59,999 --> 99:59:59,999 is most common in open source projects. 57 99:59:59,999 --> 99:59:59,999 It's because they want to optimize 58 99:59:59,999 --> 99:59:59,999 for future readership of the code base. 59 99:59:59,999 --> 99:59:59,999 A single straight line of history provides the easiest reading 60 99:59:59,999 --> 99:59:59,999 for a future contributor to this project. 61 99:59:59,999 --> 99:59:59,999 This is why they put the burden on the contributors, 62 99:59:59,999 --> 99:59:59,999 to make the history clean. 63 99:59:59,999 --> 99:59:59,999 It's an effort, but one that benefits 64 99:59:59,999 --> 99:59:59,999 every future collaborator on this project. 65 99:59:59,999 --> 99:59:59,999 Continuously delivered applications, 66 99:59:59,999 --> 99:59:59,999 such as web services, and web apps, 67 99:59:59,999 --> 99:59:59,999 are optimized, generally, for merges, not rebase. 68 99:59:59,999 --> 99:59:59,999 They want the quickest possible delivery mechanism 69 99:59:59,999 --> 99:59:59,999 to send a change, small and focused, into the master branch. 70 99:59:59,999 --> 99:59:59,999 If that doesn't do everything that it should, 71 99:59:59,999 --> 99:59:59,999 another branch is worked on, and merged back in again. 72 99:59:59,999 --> 99:59:59,999 Rebase is a powerful feature 73 99:59:59,999 --> 99:59:59,999 that lets you optimize for clarity of history. 74 99:59:59,999 --> 99:59:59,999 Just bear in mind the needs of your project and your team. 75 99:59:59,999 --> 99:59:59,999 If it is for quick delivery of feature branches, 76 99:59:59,999 --> 99:59:59,999 just merge them in. 77 99:59:59,999 --> 99:59:59,999 If it's for clarity of history, engage in using Rebase. 78 99:59:59,999 --> 99:59:59,999 Thanks for watching this Git & GitHub Foundations 79 99:59:59,999 --> 99:59:59,999 episode on Rebase. 80 99:59:59,999 --> 99:59:59,999 Be sure to subscribe to our channel, over here, 81 99:59:59,999 --> 99:59:59,999 ask us questions or comments in the box below, 82 99:59:59,999 --> 99:59:59,999 or watch one of the related episodes, such as on creating a branch.