One of Git’s most impressive features is its powerful branching capabilities. Though competing tools — especially centralized ones — make a big deal out of branching and merging, these are typically easy operations in Git. However, working with an online repository host, such as GitLab, adds another layer of complexity. Since now they have to deal not only with their local branches but also with remote ones, many users often get confused and frustrated at this point. In this post, we’ll show you the main Git commands you must be aware of so you can manage your branches in an easy and efficient way.
Creating and Switching To Branches in GitLab
To create a new branch, you can use the git branch command followed by a name. So, to create a branch called “experimental,” you should run:
Then, to switch to your new branch, you can use both the checkout and the switch commands. So, the two following alternatives achieve the same result:
The checkout command is older and does a lot of things. The switch command, on the other hand, is newer. It was introduced as an experimental command in version 2.23 and is dedicated only to switching to branches. When you create a branch, most of the time you’ll also want to switch to it, so there are ways to do the two things in one go. The following example creates a new branch called “test” and immediately switches to it, using two possible alternatives:
Git Merge: Integrating Changes From Different Branches
There are a few different ways in which you can integrate changes from another branch. One of them is cherrypicking, which allows you to incorporate selected commits from another branch. We have an entire post about the git cherrypick command. Another powerful way of incorporating changes is via rebasing. We also have a post that covers the git rebase command, among many more. Merging is the most common way of bringing commits from another branch into the current one. Imagine you have a main branch with three commits. Then you create a new branch called “exp,” switch to it, and add two more commits. How can you integrate those commits into main? Easy. You switch back to main and perform a simple merge:
In this case, only one of the two sides diverged. In such a scenario, the merge operation is simpler and called a fast-forward merge.
Working With Remote Branches
We’ll now briefly cover some commands you can use to manage your branches when working with a service that offers repository hosting, such as GitLab.
Cloning a Repository
Unlike centralized version control tools, Git doesn’t have the concept of a central server. Instead, in Git you can have any number of remote repositories — or simply remotes. You can think of remotes as copies of your repositories that live somewhere else. The most common way of obtaining a remote is by cloning a repository. You can do it by running the git clone command, followed by the remote’s URL:
Adding a Remote
You can have any number of remotes you need. When working in open-source collaboration, you might need to add the original repository as a remote, for instance. Or you might want to add your coworker’s machine as a remote to do some peer collaborating. That’s easy to accomplish:
Checking Out Remote Branches
As soon as you have one or more remotes, you’ll need to check them out so you can work with them. Checking out remote branches is a bit of a misnomer; technically speaking, you can’t work with other people’s branches using Git. What you actually do is create a new branch that tracks the remote branch. The easiest way to accomplish that is to fetch from the desired remote and then use the checkout command:
Git and GitLab Branch Management: Where to Go From Here?
Git’s branching capabilities are one of the features that set it apart from its competitors. Though creating branches is cheap, managing them can be quite time-consuming, especially when you factor in remotes and remote branches. In this post, we’ve briefly covered some of the main commands you’ll typically use when you’re managing branches in Git and working with a service like GitLab to host your remotes. Of course, there’s only so much you can cover within a single blog post. If you want to learn more about Git and how to integrate it with GitLab, we suggest you take a look at Cprime’s GitLab Boot Camp.