Branching
It's time to delve into the wonderful world of branches! Branches in Git are flexible and let you do some really powerful things. Before we get into the nitty gritty details of the commands, let's take another step back and look at the big picture of what branches are and how they function.
So that's the big picture of how branches work and how to switch between branches. Did you know that you've already seen the master branch on the command line? Because of the setup files you added in the first lesson, the current branch is displayed right in the command prompt.
Thegit branch
command
Thegit branch
command is used to interact with Git's branches:
$ git branch
It can be used to:
- list all branch names in the repository
- create new branches
- delete branches
If we type out justgit branch
it will list out the branches in a repository:
The Terminal application showing the output of thegit branch
command. The master branch is displayed.
Create A Branch
To create a branch, all you have to do is usegit branch
and provide it the name of the branch you want it to create. So if you want a branch called "sidebar", you'd run this command:
$ git branch sidebar
QUESTION 1 OF 3
Remember that there are a number of branches in the repository, but that the command prompt displays thecurrent branch.
Now that we just created a new "sidebar" branch, does the command prompt displaysidebar
ormaster
?
sidebarmaster
SUBMIT: That's right! Even though you created the sidebar
branch, it's not the current branch just yet. We need to switch to it.
Thegit checkout
Command
Remember that when a commit is made that it will be added to the current branch. So even though we created the newsidebar
, no new commits will be added to it since we haven'tswitched to it, yet. If we made a commit right now, that commit would be added to themaster
branch,_not_thesidebar
branch. We've already seen this in the demo, but to switch between branches, we need to use Git'scheckout
command.
$ git checkout sidebar
It's important to understand how this command works. Running this command will:
- remove all files and directories from the Working Directory that Git is tracking
- (files that Git tracks are stored in the repository, so nothing is lost)
- go into the repository and pull out all of the files and directories of the commit that the branch points to
So this will remove all of the files that are referenced by commits in the master branch. It will replace them with the files that are referenced by the commits in the sidebar branch. This is very important to understand, so go back and read these last two sentences.
The funny thing, though, is that bothsidebar
andmaster
are pointingat the same commit, so it will look like nothing changes when you switch between them. But the command prompt will show "sidebar", now:
The Terminal application showing the "sidebar" branch in the command prompt.
Branches In The Log
The branch information in the command prompt is helpful, but the clearest way to see it is by looking at the output ofgit log
. But just like we had to use the--decorate
flag to display Git tags, we need it to display branches.
$ git
log
--oneline --decorate
This is what my log output displays (yours might look different depending on what commits you've made):
In the output above, notice how the special "HEAD" indicator we saw earlier has an arrow pointing to the sidebar branch. It's pointing to sidebar because the sidebar branch is the current branch, and any commits made right now will be added to the sidebar branch.
The Active Branch
The command prompt will display the_active_branch. But this is a special customization we made to our prompt. If you find yourself on a different computer, the_fastest_way to determine the active branch is to look at the output of thegit branch
command. An asterisk will appear next to the name of the active branch.
QUESTION 2 OF 3
From what you know about both thegit branch
andgit tag
commands, what do you think the following command will do?
$ git branch alt-sidebar-loc 42a69f
will create a branchalt
at the same commit as themaster
branchwill create the 3 branchesalt
,sidebar
,loc
will move the master branch to the commit with SHA42a69f
will create the
alt-sidebar-loc
branch and have it point to the commit with SHA42a69f
SUBMIT: That's right! It creates a new branch called alt-sidebar-loc
and has it pointing at the commit with the SHA 42a69f
Which Branch Is Active?
Given the following output fromgit branch
:
$ git branch
barbara
* footer-fix
master
richard
sidebar
social-icons
Which branch is the active branch? footer-fix
SUBMIT: That's right! Because the asterisk is next to footer-fix
, that means it's the active branch.
Delete A Branch
A branch is used to do development or make a fix to the project that won't affect the project (since the changes are made on a branch). Once you make the change on the branch, you can combine that branch into themaster
branch (this "combining of branches" is called "merging" and we'll look at it shortly).
Now after a branch's changes have been merged, you probably won't need the branch anymore. If you want to delete the branch, you'd use the-d
flag. The command below includes the-d
flag which tells Git to_delete_the provided branch (in this case, the "sidebar" branch).
$ git branch
-d
sidebar
One thing to note is that you can't delete a branch that you're currently on. So to delete thesidebar
branch, you'd have to switch to either themaster
branch or create and switch to a new branch.
Deleting something can be quite nerve-wracking. Don't worry, though. Git won't let you delete a branch if it has commits on it that aren't on any other branch (meaning the commits are unique to the branch that's about to be deleted). If you created thesidebar
branch, added commits to it, and then tried to delete it with thegit branch -d sidebar
, Git wouldn't let you delete the branch because you can't delete a branch that you're currently on. If you switched to themaster
branch and tried to delete thesidebar
branch, Git_also_wouldn't let you do that because those new commits on thesidebar
branch would be lost! To force deletion, you need to use a capital D flag -git branch -D sidebar
.
Git Branch Recap
To recap, thegit branch
command is used to manage branches in Git:
# to list all branches
$ git branch
# to create a new "footer-fix" branch
$ git branch footer-fix
# to delete the "footer-fix" branch
$ git branch
-d
footer-fix
This command is used to:
- list out local branches
- create new branches
- remove branches
Further Research
- Git Branching - Basic Branching and Merging from the Git Docs
- Learn Git Branching
- Git Branching Tutorial from the Atlassian Blog