Git is a wildly popular tool that is incredibly pervasive in the IT community at large. Git serves as a source control utility that is straightforward and lightweight. People like it because it is simple to set up, easy to use, and very efficient at what it does. One of the killer features of Git is its ability to perform branching operations with incredible speed.
For those who might not be aware, branching is simply creating a unique copy of the source files in a repository. Branches are typically used to work on a specific feature, or perhaps to address a bug or issue. In Git, a repository starts out with a single branch called master. That said, you can create as many branches as you need!
Historically, branching with a source control program was a costly operation. Think about it; the program must essentially create a copy of each file in the current repository and manage changes with respect to each copy while maintaining the master copy. For repositories containing a few dozen files (or possibly even hundreds of files), that can become a very burdensome operation. Especially as changes begin to occur exponentially.
For Git, branching is nearly instantaneous and effectively free of cost. This has a lot to do with how it is designed. Git uses snapshots to keep track of each file. When a branch is created, Git creates a pointer (or reference) to the file in the original branch. The file itself is only copied for a branch if a change is made to that file within the branch.
A Branching How-To
Keeping with the theme of lightweight, it is really easy to create a new branch in Git. The command is really straight forward! You simply use
git branch and provide your branch name, as demonstrated below:
$ git branch killer-feature
Of course, creating a branch by itself does not do a whole lot. We must move into the branch and make some changes if we really want to use use our branch. We can switch to the branch using
git checkout. Once we are in the branch, we can work with it just like we did with our master branch.
$ git checkout killer-feature Switched to branch 'killer-feature'
A handy shortcut in Git is that you can use a single command to both create and switch to a branch:
$ git checkout -b bug-fix Switched to a new branch 'bug-fix'
Due to the powerful efficiency built into Git branching, it is a favored workflow for Git. Branching enhances collaboration by allowing for multiple features or bugs to be worked on in their own branch, with minimal interference between them. Once the work is complete on a branch, it may be simply merged back into the original branch.
Want More Git?
Looking for more basics on Git? Linux Academy’s recently refreshed Git Quick Start course covers more on branching and merging, as well as the essential features for working with it in everyday life. With little more than a basic working knowledge of Linux, you can quickly build a foundational understanding of the Git source control utility. Check out the hands-on course here.