Now in the last lesson we spoke about some of the more basic variants of version control.
Now in this lesson I want to talk about branches and branching.
Now let's start off with a simple example say if we had version 1 and 2,
so two commits that were made to our local repository.
Now at this point we realize that we want to maybe try out something different, maybe build a new feature
or just to mess around with a new idea or concept.
So what we can do is instead of continuing to commit to the master branch which is the main branch that
you see here, we can also create a side branch. So we can create as many branches as we like.
So after the second commit, we create a new branch and we start committing to this new branch or this
experimental branch. We add some features and we write some code.
Now simultaneously we can continue working on the main branch putting out all those essential updates
or bits of code that are maintaining our main project but at the same time we can continue to update
and work on this experimental branch trying things out and committing our experiments to this experimental
branch.
So now we have two branches that are parallel to each other and they can be developed simultaneously.
Now if at some point in the future that we decide that that experiment was really fruitful and the feature
that we built in it was really really great and we'd like to merge it back to the main branch or to
the main project, then that can be done really easily as well by simply placing a merge request in.
And we can bring all of those changes that we experimented with, that we messed around with back to the
main project and check to see if there's any conflicts with the main branch code.
And if not or if after a little bit of editing then we can bring all of those changes into the main
working master branch. And then we can continue working from here on to the next commit or we can make
more branches. And very often what you see in practice is that there'll be multiple branches being worked
on at the same time for any given large project.
And the reason is because sometimes you are developing new features, sometimes you are fixing bugs and
all of these things may break your main project so you don't want to do it on the master branch.
You only want to put it onto the master branch once you've tested, once you know that everything is working
fine and then you can bring your code back to the working copy ready for shipment and deployment.
So let's take a look at how this would work in reality.
Now let's say we navigate back to our story directory
and inside here we've still got our previous three chapters and they are under version control.
So if we just have a look at git log you can see that this is currently the most recent commit and it's
also mirrored in our remote.
So let's try and do this locally first.
If I decided I want to create a new branch you can simply just git branch and specify the name of your
new branch.
So I'm going to add a space related plot to my story.
So let's call our branch alien-plot, hit enter. And now you can check out what branches you have by just
writing git branch without the name. And you can see that you've got one brand called alien-plot and
another one called master and the * shows you which branch you are currently on.
So we're currently on the master branch.
We can switch to the alien-plot by simply writing git checkout alien-plot.
Okay.
So as it says we have now switched to the branch called alien-plot.
Now it is inside this branch that I'm going to make some changes to my chapters.
So let's try doing that.
Now I'm going to make some completely nonsensical hum modification to my files.
So let's change chapter 1, let's change chapter 2.
Okay, here we go.
Okay.
So we've made some modifications to
chapter 1 and chapter 2 and we've decided to change some of the preexisting written text to integrate a
space themed or alien themed plot.
So let's go ahead and commit that.
So remember we have to do git add and then git commit -m.
And our message is going to be 'modify chapter 1 and 2 to have alien theme'.
OK now hit enter and we've made our commits.
So now if we do a git log, we can see that we have two commits made on the master branch and we have
one commit made on the alien-plot branch.
So now let's say that we go back on to our master branch.
So whenever you're confused where you are, you can always do a git branch to check and see where the
* is and the * is obviously where you are.
So let's do a git checkout master to go back to the master branch. So you can see that our master branch
is unchanged with respect to the space or alien-plot that we did just now.
Nothing has changed over here.
So while on the master branch I'm going to create a new file, I'm going to call it chapter4.txt.
And inside chapter 4
let's let's add something.
Okay and hit enter and save.
By the way I have no idea what I'm typing.
I'm now making up and destroying, probably in the process, a masterpiece.
But it's OK.
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
commit.
Now there's ways of combining add and commit together in the same command.
But I think if you need to git, it's always good to really know in your head exactly what's going on.
So I recommend actually separating those two bits out.
So let's go ahead and give it a commit message, add chapter r.
OK.
So git log.
Let's see what have we got.
So we are currently only looking at the master branch and we have three commits.
This is the position of our remote,
so on our GitHub repository this was the latest commit that it could see.
But on a local get repository this is in fact the latest commit, the one that we just made where we added
chapter 4.
And you can see that if I switch between the branches say if I go over to alien-plot branch and hit
enter, you can see the local files actually changed as I switch branches.
So let's just say that I'm quite happy with the changes I've made in terms of my alien-plot addition
and I would like to merge these changes back into my master branch.
So I've done a little bit of experimentation on a separate branch,
I've messed with a few things,
and I think it was a great experiment
so I'm going to put it back into our main master branch.
So in order to do that what you have to do is go back to the master branch, so git checkout master,
and while we're on the master branch we're going to merge the changes inside the alien-plot branch.
So we use the command git merge and we're going to specify the branch name which was alien-plot.
Hit enter and this opens up Vim which is a text editor and this allows you to add a merge message if
you wish.
And alternatively as we're going to do here, we're just going to leave it empty and you to write.
"
q! to save and quit. And you can see now I've actually absorbed those changes from the
alien-plot.
So I'm on my master branch and you can see that because if I do git branch, I am on the master branch.
So the alien-plot branch still exists by the way.
So on the master branch if you have a look at git log you can see that I've got some of the previous
commits but I've also got this merge branch alien-plot which was my most recent commit.
So now at this point let's do a git push to our origin master and remember to add the -u flag.
So that's completed.
And let's check it out online.
So if we go over to our story repository, you can see that there's now five commits.
So we modify chapter 1 and 2, we added chapter 4 and we merge the alien-plot branch.
So if you go into insights, graphs and go to network,
now if I can zoom in on this network graph and get rid of the tag, then you can see this is the process
that we've gone through.
So this is the master branch where we did chapter 1 and chapter 2. And then at this point after
I created chapter 2 and 3, I created a new branch.
And while inside this branch I made a commit that modified chapter 1 and 2 to have an alien theme.
And then on the master branch I continued developing the master brand adding a chapter 4.
But subsequently I realized that I did a great job on adding alien themes to our storyline and I decided
to merge it back into the main branch.
So this is basically a graphical representation of what's been going on. Now you can do everything I
just did inside GitHub itself and it's actually really easy to do.
So let's go ahead and create a brand new repository and I'm going to call this one story2 because
that's how imaginative I am, clearly not suited for story writing.
Now it's really important
we're going to leave our repository as public but you're going to check this box to initialize your repository
with a readme.
Now a readme is simply a text file that tells other people what your Git
Hub repository is all about.
So if you go on to any repository, lets just search for something like--
And we go over here,
you can see that this part that comes below the list of files is the readme. And some people format
this really heavily so that you can show various flags but you can also tell people how to use your
library, how to incorporate it into their project,
basically a whole bunch of useful stuff for
anybody who is looking at your repository.
So now we've got our new repository so you can click on the readme file and you can make some edits
if you wish.
But what we're going to do is we're going to go into our main story 2 repository and I'm just going
to create a new file.
So this is going to be again chapter1.txt and in my new file I'm just going to say, 'This is a story'.
I think I'm pretty much given up on writing stories now. And we're going to make a commit
so this is going to be create
chapter 1. And we're going to commit directly to the master branch.
So commit new file.
Ok so now that we've got chapter 1 and we are on the branch master, at this point I'm going to create
a new branch and let's call this new branch experimental.
Okay, so now we have experimental and it takes you straight to the experimental branch copying over everything
that you had at that point inside the master branch.
And now inside this experimental branch, I'm going to make an edit to my chapter 1. 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 to edit this file.
This is a story. This-- Nothing like a bit of confidence in your story writing to make it better
right?
Okay. So now let's commit these changes and let's say 'Update chapter 1 to be more upbeat'.
Okay. Now we're going to commit and we're committing, so always check to see where you're committing to
because sometimes when you have a lot of branches and you're switching branches then it can get quite
confusing.
So we're committing to the experimental branch and we're going to go ahead and commit those changes.
Now let's switch back to our master branch and you can see immediately this is reflected. So that new
line that we had previously is now gone.
So let's go back to our main repository file.
You can see that you've already got this little notification here which is telling you that you've got
a experimental branch and you can compare between your master and experimental and make a pull request.
So we're going to talk about pull requests in more detail later on.
But basically this is telling you, 'Hey you've got changes that are not reflected in your master branch. Do
you want to pull those changes over to your master branch?'
So right now that's not we're going to do.
But instead on our master branch, so check to make sure that master is checked, we're going to create a
new file and we're going to call it chapter2.txt. And we're going to say, 'This is an even better
chapter.' Okay.
So let's say, 'Create chapter 2' and we're committing to the master branch.
So commit that file and then we're going to check out what the structure of our repository looks like
now inside graphs and inside network. So you can see that we've got a initial commit and then we have
our created chapter 1 and then after chapter 1 we decide to make a branch that was experimental and
then we continued where we left off in the master branch.
So now what I want to do is I want to go into that master branch and I want to merge those changes in
the experimental branch over.
So I want to pull those changes from the experimental branch to the master branch.
So, in order to pull you have to be in the place that you're going to do the pulling which is the master
branch.
And while we're here I'm going to say New pull request and this takes me to the compare page.
So here I can say that my base is my master branch and I want to compare it with my experimental branch.
So we can see that the difference is only this line, so plus this line. When it's red, it's an erasure and
when it's a plus it's an addition.
So whereas inside my master branch, this is all I've got inside chapter 1. In my experimental branch
I've got all of this.
So I have to say that I prefer this version in the experimental branch.
So I'm very happy with what I did in the experiment
so I'm going to create a pull request that is going to merge these two branches.
So now once I've done that I'm going to merge those two branches.
So I'm going to say merge experimental branch to master and go ahead and confirm the merge.
Now you can see it's almost more straightforward doing it on the terminal, but you get used to the interface
and this is not so complex either.
So now if we have a look at our network graph,
you can see that the experimental branch was pulled back to the master branch and merged.
So that was us creating a branch and merging it.
Now why don't you go ahead and have a go and create your own repository both locally and as well as
on GitHub
and you know write a story or write a poem anything, you like and check out the amazingness that is
branching using Git.