Instructor: Now in the last lesson,
we spoke about some of the more basic variants
of version control.
In this lesson, I wanna talk about branches and branching.
Now let's start off with a simple example.
Say if we had version one and two,
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 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 on to 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 asterisk 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 gonna make some completely nonsensical modification
to my files.
So let's change chapter one.
Let's change chapter two.
Okay, there we go.
Okay, so we've made some modifications to chapter one,
chapter two
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 one
and two 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 asterisk is
and the asterisk 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 gonna call it chapter4.txt.
And inside chapter four, let's add something.
Okay and hit Enter and say...
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 chapter four on our main branch
and I'm gonna 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
and chapter four.
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 chapter four.
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 switch 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 gonna do here
which is gonna leave it empty,
and you're gonna 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 domain
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 modify chapter one and two,
we added chapter four
and we merge 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 one
and chapter two.
And then at this point
after I created chapter two and three,
I created a new branch.
And while inside this branch, I made a commit
that modified chapter one and two
to have an alien theme.
And then on the main branch,
I continued developing the main branch
adding a chapter four.
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.