0 1 00:00:00,330 --> 00:00:05,490 Now in the last lesson we spoke about some of the more basic variants of version control. 1 2 00:00:05,490 --> 00:00:09,380 Now in this lesson I want to talk about branches and branching. 2 3 00:00:09,540 --> 00:00:15,540 Now let's start off with a simple example say if we had version 1 and 2, 3 4 00:00:15,570 --> 00:00:19,520 so two commits that were made to our local repository. 4 5 00:00:19,530 --> 00:00:25,950 Now at this point we realize that we want to maybe try out something different, maybe build a new feature 5 6 00:00:26,130 --> 00:00:29,340 or just to mess around with a new idea or concept. 6 7 00:00:29,370 --> 00: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 8 00:00:36,150 --> 00:00:43,180 you see here, we can also create a side branch. So we can create as many branches as we like. 8 9 00:00:43,200 --> 00:00:51,270 So after the second commit, we create a new branch and we start committing to this new branch or this 9 10 00:00:51,330 --> 00:00:56,190 experimental branch. We add some features and we write some code. 10 11 00:00:56,190 --> 00:01:02,880 Now simultaneously we can continue working on the main branch putting out all those essential updates 11 12 00:01:02,940 --> 00:01:11,070 or bits of code that are maintaining our main project but at the same time we can continue to update 12 13 00:01:11,130 --> 00:01:17,490 and work on this experimental branch trying things out and committing our experiments to this experimental 13 14 00:01:17,490 --> 00:01:18,200 branch. 14 15 00:01:18,270 --> 00:01:24,900 So now we have two branches that are parallel to each other and they can be developed simultaneously. 15 16 00:01:24,990 --> 00:01:31,380 Now if at some point in the future that we decide that that experiment was really fruitful and the feature 16 17 00:01:31,380 --> 00: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 18 00:01:37,500 --> 00:01:44,340 the main project, then that can be done really easily as well by simply placing a merge request in. 18 19 00:01:44,520 --> 00:01:50,970 And we can bring all of those changes that we experimented with, that we messed around with back to the 19 20 00:01:50,970 --> 00:01:55,860 main project and check to see if there's any conflicts with the main branch code. 20 21 00:01:55,860 --> 00: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 22 00:02:02,310 --> 00:02:10,620 working master branch. And then we can continue working from here on to the next commit or we can make 22 23 00:02:10,620 --> 00:02:17,220 more branches. And very often what you see in practice is that there'll be multiple branches being worked 23 24 00:02:17,220 --> 00:02:20,700 on at the same time for any given large project. 24 25 00:02:20,700 --> 00:02:27,420 And the reason is because sometimes you are developing new features, sometimes you are fixing bugs and 25 26 00:02:27,450 --> 00: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 27 00:02:33,150 --> 00: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 28 00:02:40,020 --> 00:02:46,880 fine and then you can bring your code back to the working copy ready for shipment and deployment. 28 29 00:02:46,890 --> 00:02:51,020 So let's take a look at how this would work in reality. 29 30 00:02:51,030 --> 00:02:55,290 Now let's say we navigate back to our story directory 30 31 00:02:57,840 --> 00:03:04,050 and inside here we've still got our previous three chapters and they are under version control. 31 32 00:03:04,050 --> 00: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 33 00:03:10,770 --> 00:03:14,230 also mirrored in our remote. 33 34 00:03:14,480 --> 00:03:18,400 So let's try and do this locally first. 34 35 00:03:18,710 --> 00: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 36 00:03:27,020 --> 00:03:27,960 new branch. 36 37 00:03:28,130 --> 00:03:33,420 So I'm going to add a space related plot to my story. 37 38 00:03:33,530 --> 00: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 39 00:03:44,950 --> 00:03:51,460 writing git branch without the name. And you can see that you've got one brand called alien-plot and 39 40 00:03:51,460 --> 00:03:57,400 another one called master and the * shows you which branch you are currently on. 40 41 00:03:57,400 --> 00:03:59,560 So we're currently on the master branch. 41 42 00:03:59,560 --> 00:04:07,710 We can switch to the alien-plot by simply writing git checkout alien-plot. 42 43 00:04:10,610 --> 00:04:11,120 Okay. 43 44 00:04:11,160 --> 00:04:16,190 So as it says we have now switched to the branch called alien-plot. 44 45 00:04:16,230 --> 00:04:21,950 Now it is inside this branch that I'm going to make some changes to my chapters. 45 46 00:04:22,140 --> 00:04:24,630 So let's try doing that. 46 47 00:04:24,810 --> 00:04:32,850 Now I'm going to make some completely nonsensical hum modification to my files. 47 48 00:04:32,850 --> 00:04:37,560 So let's change chapter 1, let's change chapter 2. 48 49 00:04:44,600 --> 00:04:46,560 Okay, here we go. 49 50 00:04:46,650 --> 00:04:46,890 Okay. 50 51 00:04:46,900 --> 00:04:48,430 So we've made some modifications to 51 52 00:04:48,430 --> 00:04:55,990 chapter 1 and chapter 2 and we've decided to change some of the preexisting written text to integrate a 52 53 00:04:55,990 --> 00:04:59,440 space themed or alien themed plot. 53 54 00:04:59,440 --> 00:05:02,590 So let's go ahead and commit that. 54 55 00:05:02,620 --> 00:05:09,640 So remember we have to do git add and then git commit -m. 55 56 00:05:09,820 --> 00:05:24,670 And our message is going to be 'modify chapter 1 and 2 to have alien theme'. 56 57 00:05:25,010 --> 00:05:29,620 OK now hit enter and we've made our commits. 57 58 00:05:29,650 --> 00: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 59 00:05:37,330 --> 00:05:41,320 one commit made on the alien-plot branch. 59 60 00:05:41,320 --> 00:05:44,840 So now let's say that we go back on to our master branch. 60 61 00:05:44,950 --> 00: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 62 00:05:50,920 --> 00:05:54,670 * is and the * is obviously where you are. 62 63 00:05:54,940 --> 00: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 64 00:06:04,750 --> 00:06:10,130 is unchanged with respect to the space or alien-plot that we did just now. 64 65 00:06:10,150 --> 00:06:12,330 Nothing has changed over here. 65 66 00:06:12,520 --> 00: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 67 00:06:22,520 --> 00:06:24,700 And inside chapter 4 67 68 00:06:25,170 --> 00:06:28,160 let's let's add something. 68 69 00:06:32,880 --> 00:06:35,080 Okay and hit enter and save. 69 70 00:06:35,100 --> 00:06:36,800 By the way I have no idea what I'm typing. 70 71 00:06:36,840 --> 00:06:41,200 I'm now making up and destroying, probably in the process, a masterpiece. 71 72 00:06:41,340 --> 00:06:42,260 But it's OK. 72 73 00:06:42,480 --> 00: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 74 00:06:49,020 --> 00:06:50,350 commit. 74 75 00:06:50,910 --> 00:06:55,110 Now there's ways of combining add and commit together in the same command. 75 76 00:06:55,170 --> 00: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 77 00:07:00,060 --> 00:07:03,250 So I recommend actually separating those two bits out. 77 78 00:07:03,270 --> 00:07:09,550 So let's go ahead and give it a commit message, add chapter r. 78 79 00:07:10,070 --> 00:07:10,550 OK. 79 80 00:07:10,560 --> 00:07:11,810 So git log. 80 81 00:07:12,030 --> 00:07:13,320 Let's see what have we got. 81 82 00:07:13,500 --> 00:07:18,200 So we are currently only looking at the master branch and we have three commits. 82 83 00:07:18,270 --> 00:07:21,960 This is the position of our remote, 83 84 00:07:21,960 --> 00:07:27,220 so on our GitHub repository this was the latest commit that it could see. 84 85 00:07:27,360 --> 00: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 86 00:07:33,440 --> 00:07:34,350 chapter 4. 86 87 00:07:34,620 --> 00: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 88 00:07:41,130 --> 00:07:46,770 enter, you can see the local files actually changed as I switch branches. 88 89 00:07:46,770 --> 00: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 90 00:07:54,090 --> 00:07:59,300 and I would like to merge these changes back into my master branch. 90 91 00:07:59,310 --> 00:08:02,940 So I've done a little bit of experimentation on a separate branch, 91 92 00:08:03,030 --> 00:08:05,430 I've messed with a few things, 92 93 00:08:05,430 --> 00:08:07,620 and I think it was a great experiment 93 94 00:08:07,620 --> 00:08:12,160 so I'm going to put it back into our main master branch. 94 95 00:08:12,240 --> 00: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 96 00:08:20,760 --> 00:08:27,990 and while we're on the master branch we're going to merge the changes inside the alien-plot branch. 96 97 00:08:27,990 --> 00:08:37,050 So we use the command git merge and we're going to specify the branch name which was alien-plot. 97 98 00:08:37,260 --> 00: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 99 00:08:43,470 --> 00:08:44,170 you wish. 99 100 00:08:44,310 --> 00: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 101 00:08:48,750 --> 00:08:49,440 " 101 102 00:08:49,460 --> 00:08:56,910 q! to save and quit. And you can see now I've actually absorbed those changes from the 102 103 00:08:56,910 --> 00:08:57,940 alien-plot. 103 104 00:08:58,110 --> 00: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 105 00:09:06,510 --> 00:09:09,090 So the alien-plot branch still exists by the way. 105 106 00:09:09,180 --> 00: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 107 00:09:15,000 --> 00:09:21,530 commits but I've also got this merge branch alien-plot which was my most recent commit. 107 108 00:09:21,550 --> 00: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 109 00:09:32,550 --> 00:09:33,840 So that's completed. 109 110 00:09:33,840 --> 00:09:35,840 And let's check it out online. 110 111 00:09:35,880 --> 00:09:41,900 So if we go over to our story repository, you can see that there's now five commits. 111 112 00:09:41,940 --> 00:09:48,660 So we modify chapter 1 and 2, we added chapter 4 and we merge the alien-plot branch. 112 113 00:09:48,690 --> 00:09:52,410 So if you go into insights, graphs and go to network, 113 114 00:09:52,440 --> 00: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 115 00:10:00,750 --> 00:10:02,000 that we've gone through. 115 116 00:10:02,340 --> 00:10:09,390 So this is the master branch where we did chapter 1 and chapter 2. And then at this point after 116 117 00:10:09,390 --> 00:10:14,100 I created chapter 2 and 3, I created a new branch. 117 118 00:10:14,280 --> 00:10:20,410 And while inside this branch I made a commit that modified chapter 1 and 2 to have an alien theme. 118 119 00:10:20,610 --> 00:10:26,500 And then on the master branch I continued developing the master brand adding a chapter 4. 119 120 00:10:26,760 --> 00:10:36,180 But subsequently I realized that I did a great job on adding alien themes to our storyline and I decided 120 121 00:10:36,180 --> 00:10:39,770 to merge it back into the main branch. 121 122 00:10:39,990 --> 00:10:45,900 So this is basically a graphical representation of what's been going on. Now you can do everything I 122 123 00:10:45,900 --> 00:10:51,170 just did inside GitHub itself and it's actually really easy to do. 123 124 00:10:51,240 --> 00: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 125 00:10:58,380 --> 00:11:03,870 that's how imaginative I am, clearly not suited for story writing. 125 126 00:11:03,900 --> 00:11:04,850 Now it's really important 126 127 00:11:04,860 --> 00: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 128 00:11:10,610 --> 00:11:12,310 with a readme. 128 129 00:11:12,420 --> 00:11:17,440 Now a readme is simply a text file that tells other people what your Git 129 130 00:11:17,430 --> 00:11:19,650 Hub repository is all about. 130 131 00:11:19,680 --> 00:11:28,020 So if you go on to any repository, lets just search for something like-- 131 132 00:11:28,230 --> 00:11:29,880 And we go over here, 132 133 00:11:29,950 --> 00:11:36,300 you can see that this part that comes below the list of files is the readme. And some people format 133 134 00:11:36,340 --> 00:11:43,990 this really heavily so that you can show various flags but you can also tell people how to use your 134 135 00:11:44,410 --> 00:11:47,470 library, how to incorporate it into their project, 135 136 00:11:47,470 --> 00:11:49,150 basically a whole bunch of useful stuff for 136 137 00:11:49,150 --> 00:11:51,870 anybody who is looking at your repository. 137 138 00:11:52,270 --> 00: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 139 00:11:58,210 --> 00:11:58,950 if you wish. 139 140 00:11:59,080 --> 00: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 141 00:12:04,660 --> 00:12:06,420 to create a new file. 141 142 00:12:06,520 --> 00: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 143 00:12:15,580 --> 00:12:20,880 I think I'm pretty much given up on writing stories now. And we're going to make a commit 143 144 00:12:20,950 --> 00:12:23,950 so this is going to be create 144 145 00:12:24,010 --> 00:12:28,320 chapter 1. And we're going to commit directly to the master branch. 145 146 00:12:28,390 --> 00:12:30,170 So commit new file. 146 147 00:12:30,790 --> 00: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 148 00:12:37,030 --> 00:12:41,510 a new branch and let's call this new branch experimental. 148 149 00:12:41,820 --> 00:12:48,610 Okay, so now we have experimental and it takes you straight to the experimental branch copying over everything 149 150 00:12:48,610 --> 00:12:52,560 that you had at that point inside the master branch. 150 151 00:12:52,570 --> 00: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 152 00:12:58,180 --> 00: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 153 00:13:05,470 --> 00:13:14,690 This is a story. This-- Nothing like a bit of confidence in your story writing to make it better 153 154 00:13:14,690 --> 00:13:15,580 right? 154 155 00:13:15,800 --> 00:13:25,480 Okay. So now let's commit these changes and let's say 'Update chapter 1 to be more upbeat'. 155 156 00:13:26,040 --> 00: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 157 00:13:31,730 --> 00:13:36,170 because sometimes when you have a lot of branches and you're switching branches then it can get quite 157 158 00:13:36,170 --> 00:13:37,190 confusing. 158 159 00:13:37,220 --> 00:13:43,340 So we're committing to the experimental branch and we're going to go ahead and commit those changes. 159 160 00:13:43,340 --> 00:13:50,450 Now let's switch back to our master branch and you can see immediately this is reflected. So that new 160 161 00:13:50,450 --> 00:13:53,560 line that we had previously is now gone. 161 162 00:13:53,660 --> 00:13:56,150 So let's go back to our main repository file. 162 163 00:13:56,180 --> 00:14:00,470 You can see that you've already got this little notification here which is telling you that you've got 163 164 00:14:00,560 --> 00:14:08,560 a experimental branch and you can compare between your master and experimental and make a pull request. 164 165 00:14:08,630 --> 00:14:11,740 So we're going to talk about pull requests in more detail later on. 165 166 00:14:11,900 --> 00:14:17,450 But basically this is telling you, 'Hey you've got changes that are not reflected in your master branch. Do 166 167 00:14:17,450 --> 00:14:20,980 you want to pull those changes over to your master branch?' 167 168 00:14:20,990 --> 00:14:22,810 So right now that's not we're going to do. 168 169 00:14:22,880 --> 00: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 170 00:14:28,850 --> 00: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 171 00:14:39,610 --> 00:14:42,440 chapter.' Okay. 171 172 00:14:42,450 --> 00:14:51,190 So let's say, 'Create chapter 2' and we're committing to the master branch. 172 173 00:14:51,450 --> 00:14:57,180 So commit that file and then we're going to check out what the structure of our repository looks like 173 174 00:14:57,180 --> 00: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 175 00:15:06,460 --> 00:15:15,270 our created chapter 1 and then after chapter 1 we decide to make a branch that was experimental and 175 176 00:15:15,270 --> 00:15:19,070 then we continued where we left off in the master branch. 176 177 00:15:19,080 --> 00: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 178 00:15:26,670 --> 00:15:28,500 the experimental branch over. 178 179 00:15:28,530 --> 00:15:33,850 So I want to pull those changes from the experimental branch to the master branch. 179 180 00:15:34,140 --> 00: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 181 00:15:40,080 --> 00:15:40,860 branch. 181 182 00:15:41,010 --> 00: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 183 00:15:47,400 --> 00: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 184 00:15:54,600 --> 00: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 185 00:16:02,520 --> 00:16:04,650 when it's a plus it's an addition. 185 186 00:16:04,650 --> 00:16:11,760 So whereas inside my master branch, this is all I've got inside chapter 1. In my experimental branch 186 187 00:16:11,760 --> 00:16:13,190 I've got all of this. 187 188 00:16:13,320 --> 00:16:17,480 So I have to say that I prefer this version in the experimental branch. 188 189 00:16:17,520 --> 00:16:21,160 So I'm very happy with what I did in the experiment 189 190 00:16:21,270 --> 00:16:27,600 so I'm going to create a pull request that is going to merge these two branches. 190 191 00:16:27,600 --> 00:16:31,970 So now once I've done that I'm going to merge those two branches. 191 192 00:16:32,070 --> 00:16:42,450 So I'm going to say merge experimental branch to master and go ahead and confirm the merge. 192 193 00:16:42,450 --> 00: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 194 00:16:49,310 --> 00:16:51,290 and this is not so complex either. 194 195 00:16:51,330 --> 00:16:54,390 So now if we have a look at our network graph, 195 196 00:16:57,540 --> 00:17:03,990 you can see that the experimental branch was pulled back to the master branch and merged. 196 197 00:17:04,080 --> 00:17:07,730 So that was us creating a branch and merging it. 197 198 00:17:07,740 --> 00: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 199 00:17:13,890 --> 00:17:14,800 on GitHub 199 200 00:17:15,000 --> 00:17:23,280 and you know write a story or write a poem anything, you like and check out the amazingness that is 200 201 00:17:23,340 --> 00:17:25,010 branching using Git.