Merging branches together

Ian Schoonover
A free video tutorial from Ian Schoonover
Full Stack Developer and Coding Instructor
4.6 instructor rating • 4 courses • 812,075 students

Lecture description

Learn how to merge one branch into another.

Learn more from the full course

Intro to Git

A fast and easy guide to version control

55:32 of on-demand video • Updated July 2018

  • Track and revise code using Git
  • Push (upload) code to GitHub
  • Save yourself, your company, or your clients hours of wasted time and headaches from trying to rebuild after not backing up and tracking your code.
English [Auto] All right. Welcome back. So in the last video we showed you how to change from one branch into another and then back again in this video. We'll talk a little bit more about branches how they work and how we can merge them together. So if we look at this little diagram that I created we can see that these zeros represent commits. So this will be our initial command and this line here will be our master branch and at some point the second command we decided hey let's go ahead and open up a new branch called feature one. So we branch off and this line here is representing the feature one branch so we can see that feature one has commits of its own. And then we went back to master at some point and continued to make comments there. So these commits are all different from one another. And at some point we're going to want to merge either master into a feature or vice versa. Get these comments on to one timeline. So let's see what happens when we switch over to the feature branch and we start creating some new commits. Right now we can see that we're inside of M.. So we use the kid check out feature 1 command and that'll get us into the feature one branch. Now if we do a good log we can see that the latest commit is update me which is one that I created whenever I update the read me with this new information and it has all the previous commits from the master branch. So currently master branch and feature branch are in the same place. Now if we make some changes on the feature branch then we commit them. Let's see what happens. So we will touch a Final Five issue mail and if we do it status we can see that we have a contract for five. So it had five days to go check our status again. We can see that's in the staging area and we'll go and committee and we'll say five days now. Now if we check our get log you can see our most recent commit and file five days tomorrow. Now what happens if we go back to the master branch. Will this can it be there we'll use it check out master to get us back on to the master branch. And now we'll check our blog. And not only do we see 4:5 disappear on the left hand side inside of our tree. We also can see that the commit for adding 4:5 no longer exists. Interesting so if we go back and get check out feature one branch you can fall five re-appears and if we check the log there's a file five that is Chimo. So this commit right here it would represent this zero on this feature one branch. So the zero up here is not the same as this zero down here on the master branch. So currently this is what our branch system looks like. We have some comments on them master branch and then we branch off and on future one we have a new command that Master knows nothing about. So now if we go back to master and we create a new file it will say touch file six page now we check our status and we add files six days to my own bank and I still commit message here at five or six days you know and we check our get log we can see that update commit that I created and then our latest one. Add files six studies to you know this is inside of the master branch. So if we go back and check out feature 1 and do we get log. We see that 5:5 that issue now what we don't see the files six days Chimo in the file six days Chimo no longer exists. So what's happened here is we've created a another commit on the master branch that feature one branch knows nothing about. So the question is how are we going to get it to where the master branch knows about feature one branch the committees that are on that branch anyway or vice versa. What we want to do is bridge the gap here by merging these branches together and then we'll have a commit that ties them all together. So you can see here we have maß branch and we have each one branch and we want to bridge between the two branches. And what good will do is it will create a new commit that ties in that commits for both the branches together so that now we have a full history of not only feature one branch changes and commits but also master branch changes and commits. And that will be reflected in this newly created commit that it will create automatically for us when we use the merge command. So how do we do that. Well currently we are in feature one. So to make every presentation like this what we need to do is type get merge and the name of the branch that we want to merge into feature one. So we can do it this way or we can do a good checkout master and we can do a merge feature one and we can merge feature one into a master. So lets say that feature one is a new form on our Web site and weve got it working exactly how we want it. So now we want to tie that code back into our master branch. Well what we would do is check out our master branch and then we would merge feature one back into it. So go ahead and go with the second option here of get merge feature one which like I said will merge feature one branch into Master. So if we hit enter what's happened here is she in New Nano has been opened up now this is how C-9 does it. Because they're using a Linux box and they have a program called Nano set up in there. Sometimes you'll see a program called vim and there's a couple other ones but they're all pretty similar. Basically what's going on here is we're inside of the message portion of a get commit and a commit message has actually already been entered in for us automatically it says merge branch feature one. Then all you have to do is exit. Down here you can see the command for exit is control access. So will type control X and we can see this message that get gives us saying merge made by the recursive strategy for five days. You know one file changed. Couple other things going on here. Let's go ahead and look at our blog and see what this means for us. So now we're on our master branch and we have this merge branch feature one commit. Also note on the left hand side we now have this file 5 that each T.M.. That's pretty cool. Now with this most recent commit is aware of the changes that were made on the feature branch and that commit. So we have this new committee that was created by good for us. And basically what we have going on here now is represented by something closer to this. So we have our feature branch and it is being merged down into our master branch. So the changes that were made here with file five days Chimo are now being merged down and represented by a new commit. The one we have here merge branch feature one and so this is that most recent one and this is what tie's feature branch back into Master branch. And we can continue on with maß branch or we can go back up to feature branch continue making new changes and new commits and then merge them again later on down the line. So that's it for merging. And then the next video will show you how to remove a branch.