Git Merge vs Git Rebase for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome I'm Rocco and today we are going to learn something very interesting in git and that is get merge and git rebase so this is going to be very easy and very basic I will go very basic and step by step so do not worry if you have no prior knowledge on these two terms or we will start from scratch so in get there are two ways to integrate changes from one branch to another for example you have two branches in your project and you want to merge the changes we have two ways one is get much and then we have git rebase so these are the two commands we use and I will show you practically as well as theoretical II how these two are used and what is the difference and how exactly you do get much and get rebase so let me create a new folder and then I will show you exactly how to do this I am going to my terminal if you are on Windows you can use get terminal or you can use the command line so here if you see I am on my user directory I will just create a new directory here using the mkdir command the for milk directory and I will say get demo one and I will physically go to this location so if I go to my users raka folder here I should have a new directory created by the name get demo one so yes you can see it is here so this is created here and I will also show you properly here and let me hide the sidebar alright so here is the folder created kid demo 1 and this is my terminal I will show you both of the these screens my terminal and the folder get demo 1 now I will switch to this folder I will say CD get demo 1 and now I am on this folder on my terminal here to initialize gate I will say gate in it so if I say get status here now you will say it is se not a git repository so far initializing or get repository here I will have to say get in it and now you can see initialized empty git repository on this folder now if you see they will be a hidden folder dot get created but as of now I cannot view hidden folder so I'm not able to see that as of now but now it is now initialized if I say get status here now you say you can see it says on branch master and no commits yet so now I have started get on this folder let me create a new file I will say touch and I will say m1 dot txt and this file is created here you can see it is here and now if I say get status here there are no commits yet so I have to commit it so if I show you three ethically let us say we started here we started here and all right and then I have created m1 dot txt here so let's say this is my first commit here and I am one branch master so this is all I am doing on master branch this is master branch and here I will say this is m1 all right so here I have created m1 dot txt and here if I say now git status shows me that there are unchecked files to track it or to add it to get I will say git add and I will say dot so that it will add all the changes and if I now say git status you can see now it is showing in green so that means it is now tracked by git but not yet committed and to commit I will say git commit minus M and give some commit message so I will say edit m1 dot txt and if I say git status it is everything is clean and if I say git log to see the history you can see it says one commit and that is added m1 dot txt so as of now we are at this position we are on our master branch and we have done the first commit and let's say this commit is called M 1 now I will create a new branch so I will say git branch feature so I will name the branch as feature this is a new branch and now this is created and to switch to the feature branch I will say git checkout feature and it says switched to branch feature so now I am on the feature branch if I say git status you can see it says on branch feature and as of now there is nothing to commit now let me now create another file here so what I have done as of now is I have created a new branch and this is the feature branch we have named it feature and this is created from the master branch so as of now we are at this position in our project now I am on feature branch let me create a new file here I will say f1 dot txt and you can see the new file is created here f1 dot txt and now if I say get status it says there are unchecked files so to add it to get I will say get head I can just say f1 dot txt as well or I can just say get add dot so that all the changes will be added if I say get status now this is now added and I have to now commit it so I can say get commit - M and give some commit message I will say added f1 dot txt and now if I say get status there is nothing to commit everything is clean and to see the history I will say git log and here you can see in git log we have two commits added m1 dot txt and this is available in feature because we created feature branch after this commit in the master so this is already present and then we have added f1 dot txt so as of now we have reached to this position where we have created the f1 commit let's say this is f1 commit on the feature branch so this is our state of the project as of now now you can see I am as of now on the feature branch if I switch to master branch you will see the changes here on the folder as well if I say git check out master so you can see as I click enter you will see the changes here so I am pressing enter and you see f1 dot txt is corn because f1 dot txt is not present in master it is own it is only present in feature branch so now I am on my staff I say get status it says on master and let's just say now I'm doing some more changes in my master bench so I will say I will create a new file called M 2 dot txt here and it is created and I will say get ahead and get commit minus M added M 2 dot txt and if I say get log now you can see I have two commits in master m1 dot txt and added M 2 dot txt so now what has happened is our master has also gone ahead after creation of the feature branch and this is let's say m2 dot M to commit so this is m1 commit and here we have him to commit so this is our current state of the project we created feature branch and we added some more commits to feature but after the feature was created we have some new changes in the master branch and master branch is as of now at this position m2 now at this point let us say the changes which have happened in the master branch are relevant to the feature branch and I want to get these changes here in the feature branch from master branch I wanted to I want to get all the new changes in the feature branch so here we have two options here we have option to get much to merge the changes using git merge and get rebase so let me show you here so if I say here I will check out to the feature branch I am on feature branch as of now and now I will say git merge master and you will see what happens here so as of now in the feature branch I have M 1 dot txt and F 1 dot txt I do not have M 2 dot txt which was created after the generation of the feature branch so I do not have this in my feature branch but I do emerging I should also get this em--to in the feature bench so if I say git merge master and just take care just see what happens in the folder here so I will press ENTER now and did you see it now has m2 dot txt as well so now this is merged and here on the command line on the terminal it says please enter a commit message to explain why this merges necessary so here I will press I on my keyboard that is the i key to insert and now I am on insert mode I can add anything here so I will say most changes from master branch in to feature branch and after you have added the message you can press Escape key on your keyboard and then press : WQ that is right and quit and an exclamation mark and say enter so you will come out and now if you say git log here you can see in the git log you can see the history it says edit m1 dot txt then we have f1 dot txt and then we have added m2 dot txt and then we also have this merge commit merge changes from master branch into feature branch so what has happened actually is here we have got a new merge commit so this is what has happened if I show you in the diagram this is now the current state so it has actually created a new merge commit so this is what git merge has done and now here we have all the new changes from the master branch as well so this is exactly what has happened in get merge now what will happen if I do get rebase so for that to demo this let me come out I will press ctrl Z on the terminal and I will go back to my main folder and now I will again create a new folder I will say MK dir get demo - and then go to the gate demo - and here as well in the folder I will go to indeed demo - folder and here it is and let me start the entire process again and show you the difference in git rebase and get merge so actually let me open a new tab of the terminal I will press command T to create a new tab so that I can show you the differences here so let me on the first term then I will go back to get them of one folder and here i am on feature branch and if i say git log this is what my logs looks like and now here on the new tab of the terminal I am on good demo - and now again I will do the same process I will say get in it to start and get here and I will say git status I am on master branch I'm on master branch and there is nothing to commit so I will create a new file m1 dot txt it has come here and I will say git add dot git commit - M added and 1 dot txt and if I say git status everything is clean if I say git log it shows me a single commit on master branch that is edit m1 dot txt so again if I show you let me just copy this so again I am here so I'm just trying to show you in a better way I am again at this position I have a master branch and I have done the first commit and we are calling this m1 commit now of I'll create our new branch again I will say git branch feature and get checkout feature to go to the feature branch and it says switch to the feature branch so now here we are at this position so as we have done earlier we have now come to this position again we have created a new feature branch right now in the feature branch I will again say if I say get status everything is clean if I say git log as of now I have a single commit that is coming from the master branch that is m1 dot txt now here I will again create a new file called f1 dot txt and add it I will say you can see F and dot txt is created here I will say git head dot and git commit minus M and the commit message added f1 dot txt and if I say get status everything is clean and I say git git log now I have two commits here so now I have come to this position so again I met this position position here in my project now I will again go back to master I will say git checkout master so you can see as I went to master as I switch to master the f1 dot txt has gone from here so master does not have f1 dot txt and in my master I will create make some new changes I will create m2 dot txt and I will add it I will commit it I will say edit m2 dot txt and that's it if I say git log now it shows me m1 dot txt and m2 dot txt two commits so again I have come to this position here now I have come to this position position I'm on my master I have two commits and this commit is not present in the feature branch M two is not present in the feature branch now again as I told you we have already seen get much we have two options here two moister jinja's from master to feature get much or get tree biscuit much we have already seen let us now see how good tree base will work so first I will check out on the feature branch and now I will say here so you can see if I say get tetanus it says I'm on feature branch now I will say git rebase master and you can also see the changes here as of now I am on feature branch that has m 1 dot txt and F 1 dot txt when I rebase it will also get the new changes from master that is M 2 dot txt so I'll press ENTER and you can see it has got M blue dot txt and here everything is successful if I say get log now you can see it says edit m1 dot txt and 2 dot txt and then here F 1 dot txt and here what has happened is so if you see here the difference when I go to get merge and on the feature file I say get log and I see the history you can see the history here this is the commit we did on master that is a first commit that also came to feature branch which is fine here we did some changes on the feature branch and here you can see this is coming after merge that is m2 dot txt and we also have a separate merge commit here so it shows a separate merge commit and it shows the fork here however if you see git rebase it is very much linear it says edit m1 dot txt first commit then this commit and then F 1 dot txt and it does not show a separate merge commit order or separate for commit here so this is very much linear so what has happened here is when we say git rebase this entire thing instead of having a merge something like this that we did with git merge when we say git rebase when I say git rebase this entire thing goes from here to the tip of the latest commit on the master and this is how it will be so this is what happens so you can see the difference here between merge and tree base so if I show you here you can see we did a merge here and this was as it is and all the changes were as it is but then the latest changes were merged into the feature branch here it took the tip of the latest merge the latest change in master and then it started the feature from there so here you can also see the changes in the history and if I go to back to the nodes here are some changes or some differences in git merge and git rebase it is a non-destructive operation so nothing is changed in both the branches so the branches remain as it is existing branches are not changed in any way and creates a new merge commit in the feature branch as we have seen here it creates a new merge commit in the feature branch when we talk about tree base it moves the entire feature branch to begin on the tip of the master branch as we have seen here it moves the entire feature branch to begin on the tip of the master branch and rewrites the project history and we get a much cleaner and a linear project history so you can see it is much linear there are no Forks it is a much cleaner project history so this is the differences between get margin git rebase I hope you have understood the very basics of git merge versus git rebase and I hope this all was very useful for you thank you for watching
Info
Channel: Automation Step by Step
Views: 51,390
Rating: 4.8968573 out of 5
Keywords: what is git merge, what is git rebase, git merge vs rebase, difference between git merge and rebase, when to use git merge, when to use git rebase, How git rebase is different from git merge, git merge and rebase for complete beginners, git beginner tutorial, git tutorials for complete beginners, learn git and github from scratch
Id: KWAZl2QHC44
Channel Id: undefined
Length: 22min 24sec (1344 seconds)
Published: Thu Sep 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.