We've learned how to create branches, now we're going to learn how to delete them.
I've already created several branches, git branch will show that list.
Now regardless of whether you've the same ones as me or not, make sure that you switch
to the master branch, so that's git checkout master.
That will put you on a master branch with me.
Once you are there, we're going to create a new branch, git branch, and then we just
provide the name of the new branch that we wanted to create.
We're going to call is one branch_to_delete.
That will make it nice and clear.
So git branch will now show that in the list.
Notice that I'm still on the master branch.
I did not create it and switch to it, I just created it, that's important.
So now we want to delete it.
It's really simple; git branch with the lowercase d option.
So -d or you can use --delete if you'd rather. Most people just use the lower case
d, and then the name of the branch that we want to delete.
So git branch with the delete option, the branch to delete.
We hit it, deleted branch, branch_to_delete, git branch, now that branch is gone.
And you may be saying wow, that's a pretty powerful thing! Obviously we would want to
be careful and not accidentally delete something that we don't want to.
That's certainly the case, anytime you are deleting anything, but git does have a few
checks in place to make sure that you don't do something stupid.
The first of those is let's create a new branch again, we will call it branch_to_delete a second time.
So there it is, this time though, let's checkout that branch, get checkout branch to delete.
So now I'm on that branch, now lets try delete it, git branch -d branch_to_delete, cannot
delete it the branch that you are currently on.
So if we are there, we can't delete it. We have to get off of this branch first, before it'll
lets us delete it.
So make sure that it's not deleting everything that's in our working directory and somehow
leaving us stranded not on a branch.
We have to get on another branch, and then we can delete the old one.
You can think of it a little bit like if you are in the tree with actual tree branches,
you wouldn't want to cut off the branch that you were on, you want to switch to another
branch before you sawed off the other tree branch.
So that's the first check that it makes.
Let me show you the next check that it makes.
Let's say that we make a commit to this new branch.
So let's just do something real simple here, let's open up the index.html file, and let's
change Welcome to Explore California to be Explore California: Tours and More.
It's kind of silly but we have just been changing the title before, so we'll stick with that for now.
It's a simple change we can make.
We'll go ahead and commit that change.
We'll use the dash -a option so that we don't have to add it in a separate step, and we'll
just say changed title for now.
We're going to delete this branch anyway, so it's not a big deal.
So git log --oneline, now you can see that commit the we've made.
So I am on now my branch_to_delete. It won't let me delete it while I am on it, lets get off of it;
git checkout, and let's do master.
So now I am switched to master, git branch will show me that I am on my master branch.
Now let's try and delete that other branch,
git branch -d branch_to_delete, it comes up and says, woah, woah, woah, hold on.
There are some things in there that are not in the branch that you're on now.
So I'm not comfortable just throwing away those commits.
That might not be a smart thing to do.
So you tell me if you really mean it, and you really want to do it, use capital D. Capital
D will tell me that it's okay to throw away all those changes that are in that branch,
but with lowercase D, it's sort of like we have a safety on it.
It's going to check and make sure that we're not losing something.
So the -d option, the branch must be fully merged into this branch. With the -D
option, then it'll delete the branch irrespective of its merged status.
So now let's use the -D option to tell it we really mean it.
Now it's deleted, git branch, and you can see that it's gone.
So it's pretty easy to delete branches but luckily git does give you a couple of checks
to make sure that you don't do something dumb.
So always use the lowercase -d option first and then let git tell you, hey, I want to
warn you about something before I actually do this.
And then you can change it for the capital D option, if that's what you really need.