Now, in the last lesson, we spoke about some of the more basic variants of version control. 1
Now, in this lesson, I want to talk about branches and branching. 2
Now, let's start off with a simple example, say, if we had version 1 and 2, 3
so two commits that were made to our local repository. 4
Now, at this point, we realize that we want to maybe try out something different, maybe build a new feature, 5
or just to mess around with a new idea or concept. 6
So what we can do is, instead of continuing to commit to the master branch, which is the main branch that 7
you see here, we can also create a side branch so we can create as many branches as we like. 8
So after the second commit, we create a new branch and we start committing to this new branch or this 9
experimental branch. We add some features and we write some code. 10
Now, simultaneously, we can continue working on the main branch, putting out all those essential updates 11
or bits of code that are maintaining our main project. But at the same time, we can continue to update 12
and work on this experimental branch trying things out and committing our experiment to this experimental 13
branch. 14
So, now we have two branches that are parallel to each other and they can be developed simultaneously. 15
Now, if at some point in the future that we decide that that experiment was really fruitful and the feature 16
that we built in it was really, really great and we'd like to merge it back to the main branch or to 17
the main project, then that can be done really easily as well by simply placing a merge request in. 18
And we can bring all of those changes that we experimented with, that we messed around with, back to the 19
main project and check to see if there's any conflicts with the main branch code, 20
and if not, or if after a little bit editing, then we can bring all of those changes into the main working 21
master branch. And then we can continue working from here onto the next commit or we can make more branches. 22
And very often what you see in practice is that there'll be multiple branches being worked on at the 23
same time for any given large project. 24
And the reason is because sometimes you're developing new features, sometimes you are fixing bugs, 25
and all of these things may break your main project, so you don't want to do it on the master branch. 26
You only want to put it on the master branch once you've tested, once you know that everything is working 27
fine, and then you can bring your code back to the working copy ready for shipment and deployment. 28
So let's take a look at how this would work in reality. 29
Now, let's say, we navigate back to our Story directory. 30
And inside here, we've still got our previous three chapters and they are under version control. 31
So if we just have to look at git log, you can see that this is currently the most recent commit and it's 32
also mirrored in our remotes. 33
So let's try and do this locally first. 34
If I decided I want to create a new branch, you can simply just write and get branch and specify the 35
name of your new branch. 36
So I'm going to add a space related plot to my story. 37
So let's call our branch alien-plot. 38
Hit enter. And now you can check out what branches you have by just writing git branch without the name 39
and you can see that you've got one brand called alien-plot and another one called master, and the asterisk 40
shows you which branch you are currently on. 41
So we're currently on the master branch. 42
We can switch to the alien-plot by simply writing "git checkout alien-plot." 43
Okay. 44
So as it says, we have now switched to the branch called alien-plot. 45
Now, it is inside this branch that I'm going to make some changes to my chapters. So let's try doing that. 46
Now, I'm going to make some completely nonsensical hum modification to my files. 47
So let's change 48
chapter1. Let's change chapter2. 49
Okay. There we go. Okay. 50
So we've made some modifications, 51
chapter1, chapter2, and we've decided to change some of the preexisting written text to integrate a 52
space themed or alien themed plot. 53
So let's go ahead and commit that. 54
So remember, we have to do git add, and then git commit -m. 55
And our message is going to be "modify chapter 1 and 2 to have alien theme." 56
Okay, now hit enter and we've made our commits. 57
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
one commit made on the alien-plot branch. 59
So, now let's say that we go back onto our master brunch. 60
So whenever you're confused where you are, you can always do a git branch to check and see where the 61
asterisk is, and the asterisk is, obviously, where you are. 62
So let's do a git checkout master to go back to the master branch. So you can see that our master branch 63
is unchanged with respect to the space or alien-plot that we did just now. 64
Nothing has changed over here. 65
So while on the master branch, I'm going to create a new file. 66
I'm going to call that chapter4.txt. And inside chapter4, 67
let's let's add something. 68
Okay, and hit enter and say-- By the way, I have no idea what I'm typing. 69
I'm now making up and destroying probably in the process a masterpiece. 70
But it's okay. 71
So we now have a chapter4 on our master branch and I'm going to go ahead and do a git add and 72
git commit. 73
Now, there's ways of combining add and commit together in the same command. 74
But I think if you're new to git, 75
it's always good to really know in your head exactly what's going on. 76
So I recommend actually separating those two gits out. 77
So let's go ahead and give it a commit message "add chapter 4." 78
Okay, so git log. 79
Let's see what have we got. 80
So we are currently only looking at the master branch and we have three commits. 81
This is the position of our remote. 82
So on our GitHub repository, this was the latest commit that it could see. 83
But on a local Git repository, this is in fact the latest commit, the one that we just made where we added 84
chapter 4. 85
And you can see that if I switch between the branches, say, if I go over to alien-plot branch and hit 86
enter, you can see there are local files actually changed as I switched branches. 87
So let's just say that I'm quite happy with the changes I've made in terms of my alien-plot addition, 88
and I would like to merge these changes back into my master branch. 89
So I've done a little bit of experimentation on a separate branch. 90
I've messed with a few things. 91
And I think it was a great experiment. 92
So I'm going to put it back into our main master branch. 93
So in order to do that, what you have to do is go back to the master branch, so "git checkout master." 94
And while we're on the master branch, we're going to merge the changes inside the alien-plot branch. 95
So we use the command git merge and we're going to specify the branch name which was alien-plot. 96
Hit enter. 97
And this opens up vim which is a text editor and this allows you to add a merge message if you wish. 98
And alternatively, as we're going to do here which is going to leave it empty, and you're going to write 99
":q!" to save and quit. And you can see now I've actually absorbed those changes from the 100
alien-plot. 101
So I'm on my master branch. And you can see that because if I do git branch, I am on the master branch. 102
So the alien-plot still exists by the way. 103
So on the master branch, if you have a look at git log, you can see that I've got some of the previous 104
commits. But I've also got this merge branch alien-plot which was my most recent commit. 105
So, now at this point, let's do a git push to our origin master and remember to add the -u flag. 106
So that's completed, 107
and let's check it out online. 108
So if we go over to our Story repository, you can see that there's now five commits. 109
So we modify chapter 1 and 2, we added up to 4, and we merged the alien-plot branch. 110
So if you're going to Insights, Graphs, and go to Network, 111
now if I zoom in on this network graph and get rid of the tag, then you can see this is the process that 112
we've gone through. 113
So this is the master branch where we did chapter 1 and chapter 2. 114
And then at this point, after I created chapter 2 and 3, I created a new branch. And while inside this 115
branch, I made a commit that modified chapter 1 and 2 to have an alien theme. 116
And then on the master branch, I continued developing the master branch adding a chapter 4. 117
But subsequently, I realized that I did a great job of adding alien themes to our storyline and I decided 118
to merge it back into the main branch. 119
So this is basically a graphical representation of what's been going on. Now you can do everything I 120
just did inside GitHub itself. And it's actually really easy to do. 121
So let's go ahead and create a brand-new repository and I'm going to call this one Story2 because 122
that's how imaginative I am, clearly not suited for story writing. 123
Now, it's really important. 124
We're going to leave our repository as public, but you're going to check this box to initialize your repository 125
with a README. 126
Now, README is simply a text file that tells other people what your GitHub repository is all about. 127
So if you gone to any repository, let's just, I don't know, search for something like-- 128
And we go over here. 129
You can see that this part that comes below the list of files is the README. And some people format 130
this really heavily so that you can show various flags. But you can also tell people how to use your 131
library, how to incorporate it into their project, 132
basically, a whole bunch of useful stuff, 133
anybody who is looking at your repository. 134
So, now we've got our new repository. So you can click on the README file and you can make some edits 135
if you wish. 136
But what we're going to do is we're going to go into our main story to repository, and I'm just going 137
to create a new file. 138
So this is going to be, again, chapter1.txt. And in my new file, I'm just going to say, 139
"This is a story." 140
I think I'm pretty much given up on writing stories now and we're going to make a commit. 141
So this is going to be 142
"Create chapter 1" and we're going to commit directly to the master branch. 143
So commit new file. 144
Okay, so now that we've got chapter1 and we are on the branch master, at this point, I'm going to create 145
a new branch, and let's call this new branch experimental. 146
Okay, so now we have experimental and it takes you straight to the experimental branch, copying over everything 147
that you had at that point inside the master branch. 148
And now inside this experimental branch, I'm going to make an edit to my chapter1. 149
So I'm going to click on the file, not over here, but over here. And I'm going to click on the pencil button 150
to edit this file. 151
"This is a story." "This--" Nothing like a bit of confidence in your story writing to make it better, 152
right? 153
Okay, so now let's commit these changes and let's say, "Update chapter 1 to be more upbeat." 154
Okay. Now, we're going to commit and we're committing. 155
So always check to see where you're committing to because sometimes when you have a lot of branches 156
and you're switching branches, then it can get quite confusing. 157
So we're committing to the experimental branch and we're going to go ahead and commit those changes. 158
Now, let's switch back to our master branch. And you can see immediately, this is reflected, so that new 159
line that we had previously is now gone. 160
So let's go back to our main repository file. 161
You can see that you've already got this little notification here which is telling you that you've got 162
a experimental branch and you can compare between your master and experimental and make a pull request. 163
So we're going to talk about pull requests in more detail later on. 164
But, basically, this is telling you, "Hey, you've got changes that are not reflected in your master branch. 165
Do you want to pull those changes over to your master branch?" 166
So right now, that's not we're going to do 167
but instead, on our master brunch, 168
So check to make sure that master is checked. 169
We're going to create a new file and we're going to call it chapter2.txt and we're going to 170
say, "This is an even better chapter." 171
Okay. So let's say, "Create chapter 2" and we're committing to the master branch. So commit that file, and then 172
we're going to check out what the structure of our repository looks like now, inside Graphs and inside 173
Network. 174
So you can see that we've got a initial commit, and then we have our created chapter1. And then after chapter1, 175
we decide to make a brunch that was experimental, and then we continued where we left off in the master 176
branch. 177
So, now what I want to do is I want to go into that master branch and I want to merge those changes in 178
the experimental branch over. 179
So I want to pull those changes from the experimental branch to the master branch. 180
So in order to pull, you have to be in the place that you're going to do the pulling which is the master 181
branch. 182
And while we're here, I'm going to say "New pull request," and this takes me to the Compare page. 183
So here I can say that my base is my master branch and I want to compare it with my experimental branch. 184
So we can see that the difference is only this line, so plus this line. When it's red, it's an erasure. 185
When it's a plus, 186
it's an addition. 187
So whereas inside my master branch, this is all I've got inside chapter1. In my experimental branch, 188
I've got all of this. 189
So I have to say that I prefer this version in the experimental branch. 190
So I'm very happy with what I did in the experiment. 191
So I'm going to create a pull request that is going to merge these two branches. 192
So, now once I've done that, I'm going to merge those two branches. 193
So I'm going to say, "Merge experimental branch to master." And go ahead and confirm the merge. 194
Now, you can see it's almost more straightforward doing it on the Terminal, but you get used to the interface, 195
and this is not so complex either. 196
So, now if we have a look at our Network graph, 197
you can see that that experimental branch was pulled back to the master branch and merged. 198
So that was us creating a branch and merging it. 199
Now, why don't you go ahead and have a go and create your own repository, both locally and as well as 200
on GitHub 201
and, you know, write a story or write a poem, anything you like, and check out the amazingness that is 202
branching using Git.