Git branches: what they are and how to use them

Git branches are among the most helpful tools for developers to build and manage projects.

They let developers work on code separately from the main project, making it easy to test ideas and work with teammates without breaking anything in the main project.

In this practical guide, we’ll walk through:

  1. What Git branches are and why developers use them
  2. Simple steps to use Git branches in your projects
  3. How git branches can improve your workflow

Whether coding solo or in a team, branches help keep everything organized and running smoothly. By the end of this guide, you’ll know exactly how to use Git branches in your projects. Let’s get started.

What are Git branches?

In Git, branches are isolated workspaces that let you experiment, build, and test changes without affecting your main code. They’re like having multiple versions of your project running side by side, each with its own history of changes.

When you’re happy with the code in a specific branch, you can safely merge it into your main project. This lets you test changes thoroughly and work on multiple features while keeping your main code stable.

Download complete GIT cheat sheet

How to use Git branches

Before diving into branches, make sure you have Git installed and ready to go. Git is free from the official site and is readily available for all major operating systems.

If you prefer a more visual way to work with Git, check out the best Git GUI clients. These tools make development more approachable by showing code changes, branches, and history in a visual interface, which can make your workflow more intuitive.

When you’re ready, let’s explore the essential commands you’ll use regularly with Git branching.

Setting up a Git repository

The first step in working with Git branches is to create a repository. Use this command to set one up:

git init

The above command creates a directory that will track your project’s changes. To verify it worked, check your repository’s status with this command:

git status

Ensure you make your first commit to establish your main branch before you start building features. This creates a clean starting point from which your branches can build.

Listing branches

In any Git project, you can view every existing branch by entering the following command:

git branch

By showing you a clear view of all your branches, you can confidently navigate between different parts of your project and keep your development plans organized.

Creating new branches

If there is no output in the terminal, don’t worry. You just need to create a new branch. Here’s how:

git checkout -b branch-name

Common branch types include:

  • Feature. For new features.
  • Bugfix. For fixing non-critical issues.
  • Hotfix. For urgent fixes that need immediate attention.
  • Release. For preparing new version releases.

Keep these in mind when you create a new branch. You should name it in a way that clearly describes what you’re working on.

For example, a name like feature/login-page might make sense if you’re building a new login feature.

With the above command, this is what you would enter:

git checkout -b feature/login-page

Clear branch names let everyone on the team instantly understand what code changes are happening.

You can still rename a Git branch later if necessary.

Switching between branches

To move to a different branch, run the following command:

git checkout branch-name

This helps you easily switch between different workspaces.

For example, if you want to switch to your login feature branch, enter the following command:

git checkout feature/login-page

To revert to the main project, use this command:

git checkout main

Merging branches

When your branches are ready to go live, you’ll want to implement those changes to your main code repository. This is where the merge command comes in handy.

Before merging the code, switch to the branch receiving the changes – usually, this is the main code.

git checkout main

Then, merge in your completed work:

git merge branch-name

Deleting branches

Once you’ve successfully merged your changes and no longer need a branch, you can delete it to clean up your workspace.

Before deleting a branch, switch to a different one, as you can’t delete your current branch.

Once done, run the following command:

git branch -d branch-name

If you want to delete an unmerged branch, you need to replace -d with -D in the command like this:

git branch -D branch-name

Git refuses to delete unmerged branches to prevent accidental loss of work. But if you’re sure you want to remove it, this command will force the delete.

Why use Git branches

Let’s explore how branches can make you a more confident coder and transform how you build, test, and deploy code.

Protect your working code

Keep your working code safe while you build new features in separate branches. You can create Git branches to try new ideas, experiment, or update sections, knowing your main code stays safe and stable.

Better collaboration

Multiple developers can work on different features simultaneously without disrupting each other. Each branch has its own space to grow and be tested before being merged into your main code.

Improved code organization

Split your project into clear, manageable pieces that everyone understands. This helps you track new features, bug fixes, and bold experiments.

Facilitate easy code review

Check different versions side-by-side and only add code to your main project when it’s been tested and meets your quality standards.

Conclusions

Now that you understand the basic Git commands for creating and tracking branches, you can improve your development workflow.

Here are a few key points to remember:

  • Branches are safe, separate spaces to experiment with code
  • Good branch naming helps keep projects organized
  • Regular merging keeps your main code up to date
  • Remove old branches to maintain a tidy workspace

This is enough knowledge about Git branches to actually give them a try. Let us know in the comments below how it goes.

Author
The author

Simon Lim

Simon is a dynamic Content Writer who loves helping people transform their creative ideas into thriving businesses. With extensive marketing experience, he constantly strives to connect the right message with the right audience. In his spare time, Simon enjoys long runs, nurturing his chilli plants, and hiking through forests. Follow him on LinkedIn.