Git MERGE vs REBASE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to this video this video starts a bit different because as the title says we want to have a look at git rebase and git merge and see how these two commands work and how we can use them the problem is that these commands well can be applied in a lot of different situations and pros and cons and so on and actually i just wanted to give you a quick example how you could use both of these for that we'll have a look at our quick example project now in the intro and then continue with the actual code because in this video we'll start with the following situation we'll have a master branch right here with two commits m1 m2 master commit 1 master comma 2 doesn't matter a lot in the end how we name them the important thing is that this example project will also come with a feature branch this feature branch is created based on the master branch so based on this last commit in the master branch basically and we implemented some stuff in there so we are working on a new feature which shouldn't be implemented into our master branch immediately at the same time our master branch is also evolving our colleagues are working on it whatever the important thing is that we also got a new commit right here so the situation is this one we started at this second commit and now both branches evolved after that we want to continue with our feature branch and finish our feature well and then the question is how can we combine these developments the m3 commit so the latest commit in our master and the latest commit in our feature branch into or back into our master branch right here this thing right here well for that we have a lot of different approaches and in this video i want to show you two ways how to achieve this one way with git merge the other way with git rebase so that was some theory let's have a look at the code now right here i am in the project and if we have a look at git branch down here in the terminal in my integrated terminal you can see that we have exactly the same structure that we saw on the slide we have two branches master and feature and if i have a look at the commits in my master branch you can see that i have three commits the third one the second one and the first commit m1 let's now check out the other branch the feature branch right here like this and also see what's in there and as you can see we have f1 so the first commit in the feature branch and important this m2 commit right here which is the last commit both branches have in common the m2 commit we also have the m1 commit down there by the way don't worry too much about the actual content up here it's just some dummy code as you can see in the feature file right here we have f1 so basically the name of our commit in the feature branch and if i switch back to the master branch once again you can close this one you can see we have m1 m2 m3 so basically the code is equal to the name of the commits the important thing now is that i want to continue my work in the feature branch as you saw it we want to create this second commit in there and then i want to kind of combine the information of both commits in my master branch for that i'll check out the feature branch down here like that and now i will add another feature let's say i'm writing some really awesome code maybe something like f2 right here so now i'm done and my feature is great and all that stuff so i will add it and of course commit it right here by the way if you're not sure about what you're doing right here have a look at this video i think it's up here because there we dive into the basics of git just a side note back to this commit which i will name to follow the naming convention f2 now so just to be sure if we have a look at the commits in our feature branch we have f2 f1 and m2 now switching to the master and entering git log once again will show us well we know it already that the master has this m3 commit as the latest commit so what we can do now is we can simply enter git merge and keep in mind we are in the master branch now so we basically tell git now which branch we want to merge with our master branch or in our master branch basically and this branch is the feature branch like that and now don't hit enter please because if you hit enter this wouldn't be a problem you would create a merge branch and you would have one last commit which combines all the changes of both branches of the latest commits of both branches but you would also merge all the other commits of your feature branch into your master branch this is not necessarily a problem this is actually quite nice because git is a tool which allows us to track how our project evolves so what different commits we had which features we implemented which features we kind of corrected afterwards and so on so if you want that just enter git merge feature here and this will be fine but i want to achieve something different as you saw on the slide this slide right here i want to have a structure which is kind of like this so our three commits in the master and after that i want to have kind of the updated information of the feature branch and because of that i will not use git merge right here but git merge dash dash squash feature so the name of the other branch because squash simply allows us to kind of summarize all the different commits so all the changes we had in the feature branch in the last commit so it kind of puts them together and then merges this last commit with the latest commit in the master branch that sounds kind of complicated but if i now simply hit enter then you can see well the automatic merge went well sounds good but it stopped before committing as requested now this sounds like a big problem but the actual solution is that you simply enter git commit minus m and now the name of this new of this merge commit which could be feature and master merged again not the most awesome naming convention here but if we hit enter and maybe clear this and now have a look at our git log well not our good lord our git lock would be better here then you can see that the latest commit so the head in our master right here indeed is our feature and master merge commit and if i go down you can see that before this commit we have the m3 commit the m2 commit and the m1 commit and if we look at the actual code now in our project you can see that the index html file right here has m1 m2 and m3 this is nice and if we go to the feature folder right here you can see that we have our two commits that we created in the feature branch so this is working nice actually we only have this one commit right here but you could say well i don't like this because wouldn't it be nicer if i would have the same structure so m3 and then my changes of the feature branch but if i would see these two changes right here so if f1 and f2 we also have a single commits in our feature branch would be this play right here which would allow me to kind of better track the changes i made and so on and there is a way to do this and for that we can use git rebase now so it's time to kind of rewind the project a bit so here we are back in our project kind of rewind it as i said and if we look at git branch once again you can see we have the two branches we are currently in the master branch and if i enter git log right here you can see that again our latest commit in the master branch is this m3 commit and if i quit this and quickly check out our feature branch right here and logged in oh maybe clear it first and then lock the information you can see that we have the first commit so we only have f1 no f2 at the moment and the feature branch is again based on m2 so we are again in the same position we were at the beginning of the first part of this video when we talked about git merge let's clear that again the thing now is that i want to use the rebase command right now and rebase works a bit different than git merge and instead of talking about rebase i just want to apply it now but keep in mind let me log in once again if i enter git log right here that our structure in the feature branch is this one so m2 second commit of the master branch and then first commit in the feature branch so i am in the feature branch and now i enter git rebase and now master that's the other branch right because the master is the branch which has an additional commit and this third commit now should now basically be the base for our feature branch now let's hit enter and see what happens if i do this something seems to be going on right here this is good and if i now clear this once again and now enter git lock we see f1 nothing new but right here we now see that the third commit kind of magically came into our commits right here in our feature branch that's important if i enter git branch once again like this you can see we are in the feature branch i'm not in the master branch and if i switch to the master branch right here and log it you can see that nothing changed in here we only have these three commits let's go back to the feature branch and let me now tell you what happened here because actually it's quite simple we were in the feature branch and we entered git rebase master so the first thing it does is git checks both branches and has a look at the last commit both branches have in common which commit was that this was our m2 commit the commit we used to create our feature branch initially then git has a look at our current branch remember we are in the feature branch so good looks at this feature branch and see what changed actually in this feature branch now i think it finds all these changes and then kind of saves these changes internally for the moment so nothing is committed or something like that it's just saved internally then git goes back to the master and sees what happened to the master so how the master evolved and what does git see right here it sees that master got an additional commit the m3 commit now git uses this m3 commit and also kind of moves the feature branch forward to this m3 commit so the feature branch is no longer based on the m2 commit but on the m3 commit and then both branches are aligned again then git simply applies this internally saved information so our f1 change this change right here and puts it on top of this m3 commit that's basically everything that's happening and why is this cool in our case well because now i can simply go right here and enter another thing in my project let's say this is now the finished code and if by now say git add like that and git commit minus m f2 like that then we can clear this once again and log the information and as we can see we have m3 f1 and f2 so this is kind of nice and now we can switch back to our master clear it and well if you listen carefully then you probably can guess what we can do right now to finally also add the changes to our master because remember that's the current status of our master branch so we have m1 m2 and m3 as the latest commit and in our feature branch we actually have the same situation the latest commit both branches have in common is this m3 commit that's what we just achieved with our rebase command so if i enter here git rebase but now not master because we are in your master at the moment but feature what will happen then is that git will again first have a look at our two branches and see what is the last commit both branches have in common in our case this is the m3 commit now then git will analyze our master branch and see what changed well actually nothing so there is nothing git has to save internally then git will go to the feature branch and see what changed right there well and what it finds there is that we added two commits f1 and f2 then it will apply these changes to our master branch and after that it would add the changes we applied to the master and as there are no changes well basically our master should now simply have the m1 the m2 the m3 and the f1 and the f2 commit let's see if this is correct by hitting enter now and by entering well maybe clear and then get log once again and as you can see we have f2 right here on top we have f1 m3 m2 and m1 and important up here we have the common head of our master and of our feature branch so these were two examples how you can use git merge and get rebase as you can see both commands work quite differently merge kind of well combines the information into one single commit at the end if you use this squash command we use whereas git rebase well simply as the name says changes the base of your code as we saw in the feature branch we suddenly didn't start after the m2 commit anymore but after the m3 commit this also brings us to kind of one of the disadvantages of this rebase command you should never use rebase in public repositories using them locally on your machine normally should be fine so if you work on your own projects but in bigger teams you can use it but it can cause serious damage in the end and a lot of work for your colleagues i won't dive into all the details about that right now but if you look at the official git documentation right here you can see this sentence right here do not rebase commits that exist outside your repository now i won't dive into this deeper in this video as it's meant to be an introduction to show you the general capabilities of git merge and git rebase but if you want to use it together with your colleagues in public repositories then make sure to have a look at this article because otherwise this could cause some problems nevertheless i think it's good to know git merge and get rebase and with that i hope you learned something interesting for you and i also hope to see you in the next video or one of the other videos here on the channel or on acadamine.com bye
Info
Channel: Academind
Views: 662,579
Rating: 4.8204546 out of 5
Keywords: git merge, git rebase, git squash, git tutorial, git branch, git commit
Id: CRlGDDprdOQ
Channel Id: undefined
Length: 16min 11sec (971 seconds)
Published: Thu Sep 06 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.