Git Tutorial 6: Branches (Create, Merge, Delete a branch)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
dear friends in this tutorial we are going to look at branches in gate let's first see why branches are needed if you have code like this where I have this file hungry dot py and let's say you are making some crazy changes on this file ok and these changes are experimental in nature so that you might throw them away hence you want to preserve your original code so in order to do that what you'll typically do is you will copy/paste this file ok and then you will make your changes here I will just call it original file so this one is my original file original file and then you will make your changes here bunch of changes here and then you run it and you kind of do your experiment with it that way your code is still saved in this original file ok but this way of working is not very efficient so get by default supports this branch concept which essentially allows you to do the same thing it allows you to create two branches of your code where you can do parallel code development on both the branches you can make experiments you can test stuff and once things look ok you can merge them back into one file ok so that's essentially what primes will do for you now if you noticed or whenever you have good directory you will see master here so master is a default branch that it will give you now I want to make experimental changes on this core so I need to create a new branch so first let's run git branch come on when you do that it will list all the branches that you have I only have master right now I am going to create a new branch called thirsty and when I do get branch I now see master and thirsty okay now first you need to make thirsty branch active you see there is a star here and this is in green color which means master is an active branch so to make thirsty branch active you will run git checkout spaced branch named thirsty now it is saying it switched to branch thirsty so now you are on a thirsty branch again when you run git branch you'll see star in front of thirsty okay I will now make my changes so if you are not hungry I want to ask are you thirsty so I will just say input odd I you thirsty and based on the user feedback are the asked user to drink water okay so let's say this is your code you ran it it works fine so now you want to commit it so you say git commit before doing it commit you always need to do you get ad and then you get commit minus M water okay so you committed your change into thirsty branch okay now let's so let's check out our master branch back and let's see what our master branch has so I just checked out my master branch and when I move my cursor here you notice that all those changes went away because master branch doesn't have those changes those changes are experimental in nature and they exist only in thirsty branch okay so masters Magda brands now if I again go back to thirsty I will be able to see those changes so you notice there is no thirsty change when I when I switch the branch I will now see this changes right so it's quite powerful all right now let me make one more change so I added water now I am going to ask my kid to drink soda as well because it's so thirsty right so I added that and I will now commit this change as well cool so now if you do get long you see soda water per groupies are all the previous changes and if you switch the branch to master you will obviously lose those changes and when you do when you do get long okay I mistakenly tired let me type it here good long I see only border Pisa you see I don't see any water and soda changes because I am on master branch and that branch doesn't have any or thirsty related for changes okay so I will keep on making my core changes in my thirsty branch like this execute the code run it and the moment when I feel that these chord changes are ready to merge I will do the mush but before I show you the marsh let me now make a chord change into master branch so if you want to make a chord change in master branch you need to check out master branch so now I'm in master branch Here I am going to add so I love to eat junk food so I'm not going to eat only samosa but pizza burger and fries as well okay and as usual you do get a git commit to add your fries we need to get log you have fries burger peas etcetera but this change eat fries doesn't exist in you're thirsty branch you can always verify by checking it out so you see there is no fries here okay so just to give you kind of pictorial view of what we just did you can take a look at this picture so we we're on master branch this this line here is our master branch and we all the circles are individual commits so you added Pisa you added burger and at this point you created a new branch called thirsty so all green colored dots are commits into thirsty branch okay so you added water juice here you are the fries here so fries is not present in thirsty branch and water in juice is not present in master branch okay so now what we are going to do is these changes look okay and I want to merge a thirsty chord changes into master in order to do that you first need to go to your base branch where you want to merge into so I want to merge thirsty chord changes into master branch hence I am going into master branch master branch first okay so master is my active branch and now I will do git merge thirsty this is that moist thirsty branch into master branch when you do that it will ask you to type in the commit message I will do : WQ that will save my commit message and my Maj is done so when I cat get log here here after fries I have one more change which says Kate branch Marge branch thirsty alright now when I look at the code here you will notice now it is a merger of these two branches I have my thirsty code as well and I have fries as well so this is explained by this picture here so until this point you were two different branches and now you merged so at this point you have fries and water and juice as well okay alright now when I look at my github repository I only have master here because I have not pushed my changes so this is the place where you see all the branches on github okay I'm going to now push the thirsty branch okay so it says good push alright so it is not alright so you will just do git push and this is failed to push reference up to the DJ because it is behind integrator remote changes okay let me do get poked is something which is not okay git push now when you do get push it's gonna post the master branch alright so right now what it did is when you do get push at any time it is just pushing the current active branch so master changes are pushed if I look at here I see all the all the changes say fries and thirsty and everything okay but now I want to help thirsty branch present here as well so for that you need to check out that branch first so now your interests around and now you do git push okay what this is saying is that upstream meaning github doesn't help thirsty branch so you want to push a new branch use this command yes I want to do that so just copy paste this command and what this is saying is set upstream origin thirsty meaning pushed thirsty to my upstream which is github so now when i refresh and take a look here I have two branches master and thirsty so when I switch the branch here thirsty look at my core this is just my thirsty branch code it doesn't have fries here and if you look at master it has everything okay so branches essentially allows you to maintain two separate copies of your code okay and those copies might have alternate versions of your code your algorithm or whatever you're trying to do all right okay now let's see how you can delete the branch okay so I'm going to show you how to delete the branch so for deleting the branch first let me create some dummy branch now last time when we created a branch we shoot two commands get branch and then get checkout you can do both the things in one come on by doing it check out - be dummy so mine what this is saying is create a new branch called dummy for me - B means branch and also check out that branch alright so now it created the me branch and it made that active as well so if you do get branch see you see a third branch called dummy and this is active ok now what I'm going to do is delete this branch so get Ranchos three branches I want to delete the me the way you do it is by running git branch - dee dee means delete dummy when you do that it says it deleted the brunt so now when I do get branch I don't have that planche anymore alright so that was all about get branches and great merging thank you for
Info
Channel: codebasics
Views: 175,494
Rating: 4.8992949 out of 5
Keywords: git branching, git branching tutorial, git branching explained, git branches, git branches explained, git branches tutorial, git merge tutorial, git checkout tutorial, git checkout branch, branches in git, creating branches in git, branches github, create branch in git, git delete branch, git tutorial branch, how to create branches github, how to create branches git, how to create merging branch git, create branch in github, git branching and merging
Id: sgzkY5vFKQQ
Channel Id: undefined
Length: 12min 43sec (763 seconds)
Published: Wed Aug 10 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.