Now, in the last lesson, we spoke about some of the more basic variants of version control.
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,
and 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.
What we can do is instead of continuing to commit to the main branch that you see here, we can also
create a side branch. 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.
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.
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 conflict 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 branch. And then we can continue working from here onto 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're 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 main branch.
You only want to put it onto the main branch
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.
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.
Let's try and do this locally first.
If I decided that I want to create a new branch, you can simply just write "git branch" and specify the
name of your new branch.
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 branch called, alien-plot and another one called main, and the asterisks (*)
shows you which branch you are currently on.
So we're currently on the main branch.
We can switch to the alien-plot by simply writing "git checkout
alien-plot".
Okay.
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.
Now I'm going to make some completely nonsensical modification to my files.
So let's change chapter 1.
Let's change chapter2.
Okay.
There we go.
Okay, so we've made some modifications to chapter1 and chapter2, and we've decided to change some
of the pre-existing written text to integrate a space-themed or alien-themed plot.
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".
Okay, now hit Enter and we've made our commits.
Now if we do a "git log", we can see that we have two commits made on the main branch and we have one
commit made on the alien plot-branch.
So now let's say that we go back onto our main branch.
Whenever you're confused where you are, you can always do a "git branch" to check and see where the asterisks
is.
And the asterisks is obviously where you are.
So let's do a "git checkout main" to go back to the main branch. So you can see that our main 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 main branch, I'm going to create a new file,
I'm going to call it chapter4.txt.
And inside chapter four,
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 okay.
So we now have a chapter4 on our main 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're new
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.
Let's go ahead and give it a commit message.
"add chapter 4."
Okay, so "git log".
Let's see,
what have we got?
We are currently only looking at the main 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 our local git repository, this is in fact the latest commit, the one that we just made where
we added chapter4.
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 that our local files actually changed as I switched branches.
Let's just say that I'm quite happy with the changes that I've made in terms of my alien-plot addition
and I would like to merge these changes back into my main branch.
So I've done a little bit of experimentation on a separate branch,
I've, you know, messed with a few things and I think it was a great experiment.
So I'm going to put it back into our main branch.
In order to do that, what you have to do is go back to the main branch.
So "git checkout main" and while we're on the main 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're going
to write, ":q!" to save and quit.
And you can see now I've actually absorbed those changes from the alien plot.
And you can see that because if I do "git branch", I am on the main branch.
The alien plot branch still exists
by the way. 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.
Now, at this point let's do a "git push" to our "origin main" and remember to add the "-u" flag.
That's completed.
And let's check it out online.
If we go over to our story repository, you can see that there's now five commits.
We modified Chapter 1 and 2.
We added chapter 4 and we merged the alien-plot branch.
So if you go into insights and go to Network now, if I zoom in on this Network graph, then you can
see this is the process that we've gone through.
This is the main 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 main branch, I continued developing the main branch, adding a chapter 4,
but subsequently, I realized that I did a great job adding alien themes to our storyline and I decided
to merge it back into the main branch.
This is basically a graphical representation of what's been going on.
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 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.