GIT: Branching Out
Nearly every VCS has some form of branching support. Branching
means, you diverge from the main line of development and continue to do work without messing with that main line. In many VCS tools, this is a somewhat expensive process, often requiring you to create a new copy of your source code directory, which can take a long time for large projects.
Consider a tree for example. A tree always has some branches along with the large trunk. Similarly, branches in Git are smaller parts of the project which need not interfere with the main branch (recall master). They have their own commits and are added to master once they are completed. The master branch is like the trunk of a tree. In most cases, the code contained in the master branch is usually the one being deployed.
Some people refer to Git's branching model as its "killer feature"
, and it certainly lets Git apart in the VCS community. Why is it so special? Git branches are incredibly lightweight, making branching operations nearly instantaneous, and switching back and forth between branches generally just as fast. Unlike many other VCSs, Git encourages workflows that branch and merge often, even multiple times in a day. Understanding and mastering this feature gives you a powerful and unique tool and can entirely change the way that you develop.
Let's consider that our awesome_project is based on a Web Development Project. You are currently on the master branch and it's going great, but you need to add a sign-in feature to your project. Now you certainly don't want this code to mess with your current code, do you? So let's give the feature its own branch. You create a new branch by using git branch
command. Check the status of the repository to make sure you don't have anything to commit.
Name the new branch signin_feature: $ git branch signin_feature
Check the status again. Hmm, we're still on the master branch. How do we switch to our new branch? Remember our friend from before, git checkout
? This command can be used to switch between branches. When you don't provide it with a hash value, Git assumes that you want to switch to a branch. Go on and switch to the new branch we just created and check the status just to be sure.
$ git checkout signin_feature
Go ahead and make a new file, file4 in this case and commit it. After you have successfully committed it, check the log. Certainly, you see the entry of the new commit sitting there.
Let's switch back to master and view the log again. Uh-oh. The commit is missing. Did we erase it? Don't worry the commit is still there. Git is smart enough to know that this commit was in the signin_feature
branch and the master branch has nothing to do with it yet.
Keep in mind that whenever you branch out, the new branch is a copy of the current branch you are on. As of now, we branched out from the master, hence all master commits were available in the log.
There is another way you can create a new branch. Our friend, git checkout
helps us bypass the branch command entirely. Go on and type the following at your command line. Let's create a new subscription feature:
$ git checkout -b subs_feature
The -b option lets you create a branch and switch to it all in one single command.
Now that we've created some branches let's learn more about managing them. We have a total of 3 branches right now, but larger projects have hundreds or even thousands of branches. How do we keep track on which branches exist?
Git lets us view all the branches in the repository by using the 'git branch' command:
$ git branch
This will give you a list of all branches along with your current branch. Let's say we decide to skip the subscription feature and delete it. Not every one of your ideas can get to the final project.
You can use the -D option in the branch command to delete your branch:
$git branch -D subs_feature
Oops! We can't delete the branch we are currently working in. Switch back to master and try again.
Phew! This was a long one. Good work. You deserve a cookie! Next up, we look into merging. What does that mean? Read on...