WEBVTT 0 00:00.330 --> 00:05.490 Now in the last lesson we spoke about some of the more basic variants of version control. 1 00:05.490 --> 00:09.380 Now in this lesson I want to talk about branches and branching. 2 00:09.540 --> 00:15.540 Now let's start off with a simple example say if we had version 1 and 2, 3 00:15.570 --> 00:19.520 so two commits that were made to our local repository. 4 00:19.530 --> 00:25.950 Now at this point we realize that we want to maybe try out something different, maybe build a new feature 5 00:26.130 --> 00:29.340 or just to mess around with a new idea or concept. 6 00:29.370 --> 00:36.150 So what we can do is instead of continuing to commit to the master branch which is the main branch that 7 00:36.150 --> 00:43.180 you see here, we can also create a side branch. So we can create as many branches as we like. 8 00:43.200 --> 00:51.270 So after the second commit, we create a new branch and we start committing to this new branch or this 9 00:51.330 --> 00:56.190 experimental branch. We add some features and we write some code. 10 00:56.190 --> 01:02.880 Now simultaneously we can continue working on the main branch putting out all those essential updates 11 01:02.940 --> 01:11.070 or bits of code that are maintaining our main project but at the same time we can continue to update 12 01:11.130 --> 01:17.490 and work on this experimental branch trying things out and committing our experiments to this experimental 13 01:17.490 --> 01:18.200 branch. 14 01:18.270 --> 01:24.900 So now we have two branches that are parallel to each other and they can be developed simultaneously. 15 01:24.990 --> 01:31.380 Now if at some point in the future that we decide that that experiment was really fruitful and the feature 16 01:31.380 --> 01:37.500 that we built in it was really really great and we'd like to merge it back to the main branch or to 17 01:37.500 --> 01:44.340 the main project, then that can be done really easily as well by simply placing a merge request in. 18 01:44.520 --> 01:50.970 And we can bring all of those changes that we experimented with, that we messed around with back to the 19 01:50.970 --> 01:55.860 main project and check to see if there's any conflicts with the main branch code. 20 01:55.860 --> 02:02.040 And if not or if after a little bit of editing then we can bring all of those changes into the main 21 02:02.310 --> 02:10.620 working master branch. And then we can continue working from here on to the next commit or we can make 22 02:10.620 --> 02:17.220 more branches. And very often what you see in practice is that there'll be multiple branches being worked 23 02:17.220 --> 02:20.700 on at the same time for any given large project. 24 02:20.700 --> 02:27.420 And the reason is because sometimes you are developing new features, sometimes you are fixing bugs and 25 02:27.450 --> 02:33.150 all of these things may break your main project so you don't want to do it on the master branch. 26 02:33.150 --> 02:40.020 You only want to put it onto the master branch once you've tested, once you know that everything is working 27 02:40.020 --> 02:46.880 fine and then you can bring your code back to the working copy ready for shipment and deployment. 28 02:46.890 --> 02:51.020 So let's take a look at how this would work in reality. 29 02:51.030 --> 02:55.290 Now let's say we navigate back to our story directory 30 02:57.840 --> 03:04.050 and inside here we've still got our previous three chapters and they are under version control. 31 03:04.050 --> 03:10.680 So if we just have a look at git log you can see that this is currently the most recent commit and it's 32 03:10.770 --> 03:14.230 also mirrored in our remote. 33 03:14.480 --> 03:18.400 So let's try and do this locally first. 34 03:18.710 --> 03:27.020 If I decided I want to create a new branch you can simply just git branch and specify the name of your 35 03:27.020 --> 03:27.960 new branch. 36 03:28.130 --> 03:33.420 So I'm going to add a space related plot to my story. 37 03:33.530 --> 03:44.890 So let's call our branch alien-plot, hit enter. And now you can check out what branches you have by just 38 03:44.950 --> 03:51.460 writing git branch without the name. And you can see that you've got one brand called alien-plot and 39 03:51.460 --> 03:57.400 another one called master and the * shows you which branch you are currently on. 40 03:57.400 --> 03:59.560 So we're currently on the master branch. 41 03:59.560 --> 04:07.710 We can switch to the alien-plot by simply writing git checkout alien-plot. 42 04:10.610 --> 04:11.120 Okay. 43 04:11.160 --> 04:16.190 So as it says we have now switched to the branch called alien-plot. 44 04:16.230 --> 04:21.950 Now it is inside this branch that I'm going to make some changes to my chapters. 45 04:22.140 --> 04:24.630 So let's try doing that. 46 04:24.810 --> 04:32.850 Now I'm going to make some completely nonsensical hum modification to my files. 47 04:32.850 --> 04:37.560 So let's change chapter 1, let's change chapter 2. 48 04:44.600 --> 04:46.560 Okay, here we go. 49 04:46.650 --> 04:46.890 Okay. 50 04:46.900 --> 04:48.430 So we've made some modifications to 51 04:48.430 --> 04:55.990 chapter 1 and chapter 2 and we've decided to change some of the preexisting written text to integrate a 52 04:55.990 --> 04:59.440 space themed or alien themed plot. 53 04:59.440 --> 05:02.590 So let's go ahead and commit that. 54 05:02.620 --> 05:09.640 So remember we have to do git add and then git commit -m. 55 05:09.820 --> 05:24.670 And our message is going to be 'modify chapter 1 and 2 to have alien theme'. 56 05:25.010 --> 05:29.620 OK now hit enter and we've made our commits. 57 05:29.650 --> 05:37.240 So now if we do a git log, we can see that we have two commits made on the master branch and we have 58 05:37.330 --> 05:41.320 one commit made on the alien-plot branch. 59 05:41.320 --> 05:44.840 So now let's say that we go back on to our master branch. 60 05:44.950 --> 05:50.920 So whenever you're confused where you are, you can always do a git branch to check and see where the 61 05:50.920 --> 05:54.670 * is and the * is obviously where you are. 62 05:54.940 --> 06:04.600 So let's do a git checkout master to go back to the master branch. So you can see that our master branch 63 06:04.750 --> 06:10.130 is unchanged with respect to the space or alien-plot that we did just now. 64 06:10.150 --> 06:12.330 Nothing has changed over here. 65 06:12.520 --> 06:21.200 So while on the master branch I'm going to create a new file, I'm going to call it chapter4.txt. 66 06:22.520 --> 06:24.700 And inside chapter 4 67 06:25.170 --> 06:28.160 let's let's add something. 68 06:32.880 --> 06:35.080 Okay and hit enter and save. 69 06:35.100 --> 06:36.800 By the way I have no idea what I'm typing. 70 06:36.840 --> 06:41.200 I'm now making up and destroying, probably in the process, a masterpiece. 71 06:41.340 --> 06:42.260 But it's OK. 72 06:42.480 --> 06:48.690 So we now have a chapter 4 on our master branch and I'm going to go ahead and do a git add and git 73 06:49.020 --> 06:50.350 commit. 74 06:50.910 --> 06:55.110 Now there's ways of combining add and commit together in the same command. 75 06:55.170 --> 07:00.050 But I think if you need to git, it's always good to really know in your head exactly what's going on. 76 07:00.060 --> 07:03.250 So I recommend actually separating those two bits out. 77 07:03.270 --> 07:09.550 So let's go ahead and give it a commit message, add chapter r. 78 07:10.070 --> 07:10.550 OK. 79 07:10.560 --> 07:11.810 So git log. 80 07:12.030 --> 07:13.320 Let's see what have we got. 81 07:13.500 --> 07:18.200 So we are currently only looking at the master branch and we have three commits. 82 07:18.270 --> 07:21.960 This is the position of our remote, 83 07:21.960 --> 07:27.220 so on our GitHub repository this was the latest commit that it could see. 84 07:27.360 --> 07:33.420 But on a local get repository this is in fact the latest commit, the one that we just made where we added 85 07:33.440 --> 07:34.350 chapter 4. 86 07:34.620 --> 07:41.100 And you can see that if I switch between the branches say if I go over to alien-plot branch and hit 87 07:41.130 --> 07:46.770 enter, you can see the local files actually changed as I switch branches. 88 07:46.770 --> 07:53.550 So let's just say that I'm quite happy with the changes I've made in terms of my alien-plot addition 89 07:54.090 --> 07:59.300 and I would like to merge these changes back into my master branch. 90 07:59.310 --> 08:02.940 So I've done a little bit of experimentation on a separate branch, 91 08:03.030 --> 08:05.430 I've messed with a few things, 92 08:05.430 --> 08:07.620 and I think it was a great experiment 93 08:07.620 --> 08:12.160 so I'm going to put it back into our main master branch. 94 08:12.240 --> 08:20.220 So in order to do that what you have to do is go back to the master branch, so git checkout master, 95 08:20.760 --> 08:27.990 and while we're on the master branch we're going to merge the changes inside the alien-plot branch. 96 08:27.990 --> 08:37.050 So we use the command git merge and we're going to specify the branch name which was alien-plot. 97 08:37.260 --> 08:43.470 Hit enter and this opens up Vim which is a text editor and this allows you to add a merge message if 98 08:43.470 --> 08:44.170 you wish. 99 08:44.310 --> 08:48.730 And alternatively as we're going to do here, we're just going to leave it empty and you to write. 100 08:48.750 --> 08:49.440 " 101 08:49.460 --> 08:56.910 q! to save and quit. And you can see now I've actually absorbed those changes from the 102 08:56.910 --> 08:57.940 alien-plot. 103 08:58.110 --> 09:06.480 So I'm on my master branch and you can see that because if I do git branch, I am on the master branch. 104 09:06.510 --> 09:09.090 So the alien-plot branch still exists by the way. 105 09:09.180 --> 09:14.580 So on the master branch if you have a look at git log you can see that I've got some of the previous 106 09:15.000 --> 09:21.530 commits but I've also got this merge branch alien-plot which was my most recent commit. 107 09:21.550 --> 09:32.290 So now at this point let's do a git push to our origin master and remember to add the -u flag. 108 09:32.550 --> 09:33.840 So that's completed. 109 09:33.840 --> 09:35.840 And let's check it out online. 110 09:35.880 --> 09:41.900 So if we go over to our story repository, you can see that there's now five commits. 111 09:41.940 --> 09:48.660 So we modify chapter 1 and 2, we added chapter 4 and we merge the alien-plot branch. 112 09:48.690 --> 09:52.410 So if you go into insights, graphs and go to network, 113 09:52.440 --> 10:00.690 now if I can zoom in on this network graph and get rid of the tag, then you can see this is the process 114 10:00.750 --> 10:02.000 that we've gone through. 115 10:02.340 --> 10:09.390 So this is the master branch where we did chapter 1 and chapter 2. And then at this point after 116 10:09.390 --> 10:14.100 I created chapter 2 and 3, I created a new branch. 117 10:14.280 --> 10:20.410 And while inside this branch I made a commit that modified chapter 1 and 2 to have an alien theme. 118 10:20.610 --> 10:26.500 And then on the master branch I continued developing the master brand adding a chapter 4. 119 10:26.760 --> 10:36.180 But subsequently I realized that I did a great job on adding alien themes to our storyline and I decided 120 10:36.180 --> 10:39.770 to merge it back into the main branch. 121 10:39.990 --> 10:45.900 So this is basically a graphical representation of what's been going on. Now you can do everything I 122 10:45.900 --> 10:51.170 just did inside GitHub itself and it's actually really easy to do. 123 10:51.240 --> 10:58.350 So let's go ahead and create a brand new repository and I'm going to call this one story2 because 124 10:58.380 --> 11:03.870 that's how imaginative I am, clearly not suited for story writing. 125 11:03.900 --> 11:04.850 Now it's really important 126 11:04.860 --> 11:10.610 we're going to leave our repository as public but you're going to check this box to initialize your repository 127 11:10.610 --> 11:12.310 with a readme. 128 11:12.420 --> 11:17.440 Now a readme is simply a text file that tells other people what your Git 129 11:17.430 --> 11:19.650 Hub repository is all about. 130 11:19.680 --> 11:28.020 So if you go on to any repository, lets just search for something like-- 131 11:28.230 --> 11:29.880 And we go over here, 132 11:29.950 --> 11:36.300 you can see that this part that comes below the list of files is the readme. And some people format 133 11:36.340 --> 11:43.990 this really heavily so that you can show various flags but you can also tell people how to use your 134 11:44.410 --> 11:47.470 library, how to incorporate it into their project, 135 11:47.470 --> 11:49.150 basically a whole bunch of useful stuff for 136 11:49.150 --> 11:51.870 anybody who is looking at your repository. 137 11:52.270 --> 11:58.210 So now we've got our new repository so you can click on the readme file and you can make some edits 138 11:58.210 --> 11:58.950 if you wish. 139 11:59.080 --> 12:04.660 But what we're going to do is we're going to go into our main story 2 repository and I'm just going 140 12:04.660 --> 12:06.420 to create a new file. 141 12:06.520 --> 12:14.900 So this is going to be again chapter1.txt and in my new file I'm just going to say, 'This is a story'. 142 12:15.580 --> 12:20.880 I think I'm pretty much given up on writing stories now. And we're going to make a commit 143 12:20.950 --> 12:23.950 so this is going to be create 144 12:24.010 --> 12:28.320 chapter 1. And we're going to commit directly to the master branch. 145 12:28.390 --> 12:30.170 So commit new file. 146 12:30.790 --> 12:36.970 Ok so now that we've got chapter 1 and we are on the branch master, at this point I'm going to create 147 12:37.030 --> 12:41.510 a new branch and let's call this new branch experimental. 148 12:41.820 --> 12:48.610 Okay, so now we have experimental and it takes you straight to the experimental branch copying over everything 149 12:48.610 --> 12:52.560 that you had at that point inside the master branch. 150 12:52.570 --> 12:58.180 And now inside this experimental branch, I'm going to make an edit to my chapter 1. So I'm going to click 151 12:58.180 --> 13:05.450 on the file not over here but over here, and I'm going to click on the pencil button to edit this file. 152 13:05.470 --> 13:14.690 This is a story. This-- Nothing like a bit of confidence in your story writing to make it better 153 13:14.690 --> 13:15.580 right? 154 13:15.800 --> 13:25.480 Okay. So now let's commit these changes and let's say 'Update chapter 1 to be more upbeat'. 155 13:26.040 --> 13:31.460 Okay. Now we're going to commit and we're committing, so always check to see where you're committing to 156 13:31.730 --> 13:36.170 because sometimes when you have a lot of branches and you're switching branches then it can get quite 157 13:36.170 --> 13:37.190 confusing. 158 13:37.220 --> 13:43.340 So we're committing to the experimental branch and we're going to go ahead and commit those changes. 159 13:43.340 --> 13:50.450 Now let's switch back to our master branch and you can see immediately this is reflected. So that new 160 13:50.450 --> 13:53.560 line that we had previously is now gone. 161 13:53.660 --> 13:56.150 So let's go back to our main repository file. 162 13:56.180 --> 14:00.470 You can see that you've already got this little notification here which is telling you that you've got 163 14:00.560 --> 14:08.560 a experimental branch and you can compare between your master and experimental and make a pull request. 164 14:08.630 --> 14:11.740 So we're going to talk about pull requests in more detail later on. 165 14:11.900 --> 14:17.450 But basically this is telling you, 'Hey you've got changes that are not reflected in your master branch. Do 166 14:17.450 --> 14:20.980 you want to pull those changes over to your master branch?' 167 14:20.990 --> 14:22.810 So right now that's not we're going to do. 168 14:22.880 --> 14:28.850 But instead on our master branch, so check to make sure that master is checked, we're going to create a 169 14:28.850 --> 14:39.280 new file and we're going to call it chapter2.txt. And we're going to say, 'This is an even better 170 14:39.610 --> 14:42.440 chapter.' Okay. 171 14:42.450 --> 14:51.190 So let's say, 'Create chapter 2' and we're committing to the master branch. 172 14:51.450 --> 14:57.180 So commit that file and then we're going to check out what the structure of our repository looks like 173 14:57.180 --> 15:06.420 now inside graphs and inside network. So you can see that we've got a initial commit and then we have 174 15:06.460 --> 15:15.270 our created chapter 1 and then after chapter 1 we decide to make a branch that was experimental and 175 15:15.270 --> 15:19.070 then we continued where we left off in the master branch. 176 15:19.080 --> 15:26.670 So now what I want to do is I want to go into that master branch and I want to merge those changes in 177 15:26.670 --> 15:28.500 the experimental branch over. 178 15:28.530 --> 15:33.850 So I want to pull those changes from the experimental branch to the master branch. 179 15:34.140 --> 15:40.080 So, in order to pull you have to be in the place that you're going to do the pulling which is the master 180 15:40.080 --> 15:40.860 branch. 181 15:41.010 --> 15:47.340 And while we're here I'm going to say New pull request and this takes me to the compare page. 182 15:47.400 --> 15:53.910 So here I can say that my base is my master branch and I want to compare it with my experimental branch. 183 15:54.600 --> 16:02.480 So we can see that the difference is only this line, so plus this line. When it's red, it's an erasure and 184 16:02.520 --> 16:04.650 when it's a plus it's an addition. 185 16:04.650 --> 16:11.760 So whereas inside my master branch, this is all I've got inside chapter 1. In my experimental branch 186 16:11.760 --> 16:13.190 I've got all of this. 187 16:13.320 --> 16:17.480 So I have to say that I prefer this version in the experimental branch. 188 16:17.520 --> 16:21.160 So I'm very happy with what I did in the experiment 189 16:21.270 --> 16:27.600 so I'm going to create a pull request that is going to merge these two branches. 190 16:27.600 --> 16:31.970 So now once I've done that I'm going to merge those two branches. 191 16:32.070 --> 16:42.450 So I'm going to say merge experimental branch to master and go ahead and confirm the merge. 192 16:42.450 --> 16:49.310 Now you can see it's almost more straightforward doing it on the terminal, but you get used to the interface 193 16:49.310 --> 16:51.290 and this is not so complex either. 194 16:51.330 --> 16:54.390 So now if we have a look at our network graph, 195 16:57.540 --> 17:03.990 you can see that the experimental branch was pulled back to the master branch and merged. 196 17:04.080 --> 17:07.730 So that was us creating a branch and merging it. 197 17:07.740 --> 17:13.890 Now why don't you go ahead and have a go and create your own repository both locally and as well as 198 17:13.890 --> 17:14.800 on GitHub 199 17:15.000 --> 17:23.280 and you know write a story or write a poem anything, you like and check out the amazingness that is 200 17:23.340 --> 17:25.010 branching using Git.