- Branches in Git are like having the ability
to create alternate universes, and in the last video,
we created our first branch by checking something out
from the logs.
You can create a branch whenever you want,
and there's a lot of really useful branch commands
that you should be comfortable with.
First, let's take a look at the two branches
that we have right now, so I'll do a git branch command,
and notice that it's showing me there are two branches.
The one with the star right here is the branch
that we're currently on, which is master,
and then we have this alternate branch
that we created in the previous video.
Now, let's say that we really like the work
that we've done so far, and we want to save
the current state of our files,
so that we can always get back to this point.
So, to create a new branch, we're just gonna use
the branch command, and then give it a name
of what we wanna call this branch.
So, it should be git branch, and then a name.
I'll call this app01.
Now, if you do a git branch command,
you'll notice that we have three branches.
The alternate branch that we created in the previous video,
our new app01 branch as well as the master branch.
Now, when you create a branch, you actually stay
in the current branch, so anything we do from now on
will continue to be in the master branch,
not the one that we just made, and that's really important.
So, think of a branch as, sort of, a save point.
You want to save the state of all the files,
remember you could be dealing with hundreds
of files and folders, and whenever you wanna create
a branch that saves a state
of everything you've done so far,
you use this branch command.
Now, if you want to work on this current branch,
because another possibility is,
that you're working on a project and you want to test out
a new feature,
but you want to leave the current branch as is,
you may wanna leave, like, the master branch untouched,
and you wanna go play around.
So, we can do a git checkout
and then the name of the branch,
so I'll do app01, and notice that it says
that we switched to branch app01,
if you do a git branch command,
you'll see that it is the item selected,
and anything we do on our files from now on,
will be added to this app01 branch.
So, let's go ahead and make some changes.
I'll modify this to be Revision Four,
and, we'll try one more pangram here.
So, I'm gonna save this, and I'm gonna do a git add command
and a git commit.
And now, the current branch, which is app01,
has these changes.
So, if we do a git log, let's go ahead
and clear this out first and do a git log,
you could see that we have the four different commits
that we've done with the revision four commit
that we just did at the very top.
So, I'm gonna use ctrl+z to get out of this view.
And, if I want to, I can, let's go ahead and clear again,
I want to, i can go back or at least take a look
at the branches again, notice that I am in app01,
and I want to checkout now master.
git checkout master, so now I am in branch master.
If we click here, you'll notice
that we'll be in Revision Three,
git log will show us
that we just have the three different commits,
we don't have number four, and so now we have,
sort of, two versions of our project.
One that is in app01 that has some new changes
and a new commit that we've made,
and the branch master that has everything
we had done up to that point.
So, let's say that you, for example,
are working on that project,
you've created that alternate branch,
and you really like what you've done.
And so, you wanna bring those changes
back into master.
So, what we wanna do there is do a command called merge.
So, merge will take everything from one branch
and attempt to pull it into the current branch.
And it's a pretty easy command.
But, you gotta remember one thing,
you wanna do git, let's do git branch first
so we can see the two branches that we have.
So, what we wanna do is bring everything
from app01 into master, and we're noticing
that we're already in master,
so all I need to do is say git merge and then app01.
That means that I wanna take the changes
from app01, and merge them into master.
I'm already in master, so I don't need to mention master
in here at all, just remember that when you do this,
you're bringing the changes from some other branch
into the current branch.
So, if we say enter here, notice that it says Fast-forward,
index.html, one file changed, two insertions
and two deletions, so if we go back here,
we'll notice that now we have Revision Four in this branch,
and, let's do a clear, if we git log, you'll see
that we have all four revisions.
So, ctrl+z again, clear.
So, the important thing to remember with merge
is that you wanna be in the target
and merge the branch that you wanna get stuff from.
Now, there's some other merge commands
that you may find useful to learn.
Something that I do quite often is change my mind
on what the name of the branch should be.
So, you can change the name of a branch
with the -m command.
Again, let's do a git branch to look at the branches,
and say that I wanna change the name of app01,
and I just wanna call it app1.
So, to do that, I'm gonna execute the git branch command,
and I'm gonna say -m, and -m stands for move,
you're essentially copying the branch
from one name to another.
It's sort of weird, but it's the way that unix does it,
I'm not sure why -m is how we rename things.
Just the way it is though.
So, we will say, git branch -m, and then the original name,
app01, and then the new name, app1.
So, we'll hit return, and we'll do a git branch,
and you'll see, not bir branch, I'm sorry,
and let's do a git branch, and you'll see
that the app branch is now called app1,
just like we wanted it to.
Now, occasionally, you'll want to remove a branch.
A lot of times you create branches, just sort of,
as different save points as you're working
through a project.
And, sometimes you decide that you probably
don't really need that branch any more,
so to do that, you use the -D command.
So, again, let's clear, we'll do a git branch here
to look at the branches.
Let's get rid of that alternate branch
because it was really just me being crazy
and wanted to try out some different changes,
so we'll do a git branch command -D, capital D,
and then we'll put the name in here, alternate.
And, once we do that, it deletes that branch,
now that branch is gone, so sort of be careful
about deleting branches, let's go ahead and check it out.
git branch to get a list of them, and notice
that that one is gone.
So, branches are quite useful,
and they allow you to create
these really awesome save points,
and you can always sort of keep alternate versions
of your project and try out different features
and at your different branches.