Git Branches featured image

Using Git Branches: A Tutorial

Git is an open-source version control system (VCS) that can handle projects of every size. This DevOps tool is free and open-source (Git source code on GitHub). It tracks changes in the source code, allowing multiple developers to work in sync on a non-linear development. Git was first introduced back in 2005 by Linus Torvalds. This guide will showcase how to work with Git branches.

Prerequisites

It requires the following prerequisites to perform the steps in this guide:

Although we are using Linux as the platform, the Git-specific commands will work on other platforms as well.

Step 1 – Creating a Sample Repo

Git works by managing repos. A Git repo is a specialized space that Git knows how to navigate through. Git tracks changes in the repo files and updates its tracking accordingly. For demonstration purposes, we are going to work on a local repo. If you want to work on a remote repo, then it’s recommended to make a copy of it on your local system and push the changes to the remote repo.

First, create a dedicated directory for our local Git repo:

create project dir

Next, initiate the Git repo using the init command:

git init cmd

After that, create a dummy file and commit it to the repo:

git commit files

With this action, Git created the master branch on the local repo. It’s the default branch for any repo just created.

Step 2 – Listing Branches

A repo can contain multiple branches. In Git, a branch refers to a separate version of the main/original branch. The following command will print all the existing branches on the repo:

git branch

Here, the flag -a tells Git to list all the branches (even those that are not part of our local workspace).

Step 3 – Creating Branches

The default branch ( master) will be treated as the production branch. As such, it’s recommended to create a separate branch for working on the project ( developing, testing, etc.). The following Git command will create a new branch:

creating branches

Note that there can’t be multiple branches with the same name. If you try, Git will throw the following error:

branch error

Step 4 – Switching Branches

We got multiple branches on our repo. However, how can we swap the branches to work on them? To switch to a different branch, use the following command:

switching branches1

switching branches2

Note that when checking for the branch list, the current branch will be highlighted with an asterisk. What if the branch does not exist? It will throw the error instead:

checkout error

Step 5 – Making Changes on the Development Branch

In our local repo, we have the following branches so far:

  • master: The main branch that hosts the production codes.
  • develop: The development branch that the developers will work on.

Given this hierarchy, we would be working on our develop branch. How to make changes in that branch? First, switch to the develop branch:

switching branches2

For demonstration, create some additional Python scripts:

create multiple files

The next command will instruct Git to add the newly-created files to its tracklist:

Now, commit the files to the develop branch:

develop commit

The files will now exist on the develop branch only, not the master branch (not yet, at least). We’re about to find it out. Check the files of the develop branch using the ls command:

list develop files

Now, check the list of files on the master branch:

list master files

Notice the difference? The changes made to the develop branch stay there. However, we can merge the codes of these two branches so that the master branch is updated with the latest codebase from the develop branch.

Step 6 – Merging Codes between Branches

In this section, we will demonstrate how to move the code from the develop branch to the master branch. Based on our repo structure, this means that the development codes are ready to be pushed to the production line. Switch to the master branch:

list files from both

Next, we need to tell Git to merge the develop branch contents to master. The additional flag --no-ff tells Git to preserve the commit messages prior to the merge. This helps track the code changes in the future. Run the following command:

Git will open a text file in the default text editor. It will ask for a merge message (similar to the commit messages). Enter your message and close the editor:

merge msg

With the merge message, Git will commit the changes to the master branch. The output would look something like this:

git merge

Check the content of the master branch. It should contain all the changes from the develop branch:

master list after merge

Note that in most projects, it’s not recommended to directly merge the development branch with the production branch. Generally, there are three layers of branches:

  • master: The main branch hosting the production codes.
  • testing: The testing branch. It receives the codes from the development branch for the purpose of testing/debugging.
  • development: The development branch where the developers work on.

As the description suggests, before pushing to the master branch, codes from the development branch must pass the testing to ensure that the code is actually working.

Final Thoughts

This guide successfully demonstrates the basics of using branches on Git. Here, we implemented a dual-branch workflow setup and showcased how to transfer codes around. Although demonstrated with two branches, the concepts apply to multi-branch repos as well.

Interested in learning more about Git? Check out our Git-focused tutorials:

Happy Computing!