A Better Git Workflow with Rebase

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is going on guys it is jack from the modern coder and on this episode of coding tip of the day I'm rocking my classic Maryland ultimate Jersey shout out to my ultimate frisbee people even got some of my favorite discs in the wall over there anyway on this episode I'm gonna be talking about a better get workflow with rebase instead of merge so I'll start off by explaining the basic concepts of what's to rebase what's emerge and then I'll run through the actual git commands and the steps for how a rebase based workflow might look so right out of gate here the goal of both merging and rebasing is to take commits from a feature branch and put them on to a master branch or any other branch for that matter so let's start with how a quote unquote normal merge would happen so let's say I have a graph that looks like this as you can see I split off my feature branch I commit two of master and I've done a bit of work on my future branch with those two commits there now when I want to merge those two changes back onto my master branch I'm gonna run a git merge and what this will do it will take all of my feature branch changes all the changes from both of those commits and kind of stuff them into one merge commit and then put that merge commit on master or main line so after those changes are combined together into that into that merge commit your graph might look something like this and if you're working on team with other developers you can imagine that this could kind of get gross looking kind of maybe like this where you have a bunch of people working on different feature branches and they've been pushing changes and stuff like that so this is a little bit gross to look at it's confusing to trace and so this is where rebase is gonna help us out so let's jump in and see how remiss would work so let's rewind a little bit so instead of doing a git merge I'll do a rebase when I want to take my two feature branch commits and move them onto master what a rebase does is it takes all of your commits from your feature branch and basically moves them on top of the master commits like this so behind the scenes what git is doing is actually blowing away the feature branch commits and duplicating them as new commits on top of the master branch so in a sense it's rewriting history and if I open up my git log here before my rebase and then after my rebase you can see that though the commits author date and the contents of the commit itself are the same the commit IDs are different that it's created a new copy of that commit when we ran our rebase and what you get with this approach is a nice clean straight line graph with all of your commits laid out nicely in a row it makes it really easy to trace what commits went where and as if you can imagine if you're on a team with a lot of developers all the commits are still in a row so it's really easy to follow even if you have a bunch of people working on the same project at the same time at this point I think I'd better mention some drawbacks to rebase because nothing is 100% perfect rebase doesn't play super well with open source projects and pull requests just because it got me hard to trace the especially small changes introduced to a codebase and this points a little bit nuanced but I left a link to an article in the description that I think does a good job explaining it and secondly it can also be a little bit dangerous if you're working on a shared branch with other developers just because rebasing like I talked about earlier actually rewrites commits but in the workflow example I'm about to go over I'm gonna show you a way to basically mitigate this risk and for the development team I've worked on we've successfully adopted the rebase workflow I'm about to show you and we love it so that's the basics of rebase so let's now review what this might look like when you actually put it into practice so when you first start development you want to make sure you always sync the remote master and make sure you have all the changes that your teammates have been has been doing on your local machine so you just want to run and get pull and make sure you up-to-date great so I'm already up to date so when I start developing a feature you always want to check out a new branch so that all your commits can't all your commits in code can live on this branch without disrupting your master so we're just gonna get checkout dash B and we'll say you know my cool feature great so now I'm on a feature branch now I'm on my feature branch so as I'm developing along I'm gonna just I'm going to be periodically committing okay so at this point I've developed and committed two commits to my future branch so as I'm making commits on my feature on my local machine I also have my fault developers who are shipping features to our remote main line or a master branch so this means that my development environment is kind of out of sync with the remote main line and that's okay we'll fix that when we want to merge great so say now that I'm now I'm done developing my feature and I want to merge it back into master so what I want to do is check out my master my local master and I want to do a git pull so what this will do is it will make sure that my local master is up-to-date with the remote master and any of the changes that my co-workers may have pushed in the meantime as you can see I'm one commit behind so now that I have all of my co-workers changes on my local master I want to make sure that my feature branch will drive with those changes and so what I want to do here is check out my feature branch and I'll do a rebase against my master what this will do is it will reenter my feature branch against the latest changes and at this point git will let me know if I have any conflicts and if I do I can take care of them and commit all my code to my feature branch in this case I have no conflicts so I'm good to go so now that my future branch doesn't have any conflicts with my up-to-date master I'm gonna go ahead and check it out check out master and rebase master now against my feature branch and what this will do is it will take those commits on my future branch and replay them on top of my master branch so now my local main line my local master branch is nice and straight and ready to be pushed to production so at this point I should be good to go and all I have to do is a git push so if you never use the rebase workflow before I know a lot of those specific commands might be a little confusing but don't worry check out the link in the description I've written up a blog post with everything we talked about this video and so head over there if you want to take like have a reference for when you actually go and try out this rebase workflow so I think that'll do it for today's episode as always if you have a question leave it in the comments and if you have a suggestion for the next coding tip today leave it in the comments as well leave a like you found this video helpful and yeah that is it we've seen the next one peace [Music]
Info
Channel: The Modern Coder
Views: 382,070
Rating: 4.9096174 out of 5
Keywords: seattle, vlog, software developer, git, rebase, rebase git commits, git workflow, git tutorial, git rebase, git rebase example, git rebase workflow, git interactive rebase, git rebase tutorial, git interactive rebase tutorial, git rebase interactive
Id: f1wnYdLEpgI
Channel Id: undefined
Length: 6min 42sec (402 seconds)
Published: Fri Nov 24 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.