GitHub Desktop for Unity — Part 4: Branching

In the 4th part of this series, we dive into one of Git’s most powerful features—branching. 

When working on projects, it’s common to accidentally overwrite someone else’s work or struggle with testing new ideas without affecting the main project. Branches act as a safe workspace, letting you build new features without worrying about breaking your existing game build.

In this article, we’ll explore what branches are, how to create and manage them, and how you can use them for your own development workflow.

  1. Introduction
  2. Creating a Branch
    1. Creating a new branch
    2. Switching between branches
    3. Pushing and publishing commits
    4. Multiple branches and branch history
  3. Merging branches
    1. Pull request on GitHub
    2. Command line git merge
  4. Conclusion

1. Introduction

In Git, a branch is basically a copy of your project that exists alongside the main branch, where all your commits usually go. Changes you make on a branch don’t affect the project (i.e. the main branch) until you merge them back.

Branches can be used to work on new features, test ideas, or even fix bugs without affecting the stability of the existing project. A common issue in any kind of development work is that, sometimes, the work you do can accidentally cause the current build to break.

By doing your work in a branch, you ensure that any development work you do will not affect the usability of your project. Even if you break your game in your branch, you can still return back to the main branch at any time for a playable or usable version.

Branch Example

It’s also great for collaboration. Multiple team members can work on different branches simultaneously without stepping on each other’s toes, making teamwork much smoother.

2. Creating a Branch

To create a branch, you’ll first need to have an existing GitHub repository to work with. If that’s not the case for you, you’ll want to start this series from the beginning:

a. Creating a new branch

Creating a new branch in GitHub Desktop is simple. Find the Current branch dropdown on the interface and click on it, and select the branch that you want to branch from (if it’s the first time you are branch, you can only branch from main).

Once done, click on New branch:

click current branch then click new branch

Then, type in the name of the branch, and click on Create branch.

give the new branch a suitable name then click create branch

Once the branch is created, you can use it like any regular repository. Any changes you make will be tracked, and you can make commits whenever you are done with your work.

b. Switching between branches

After your new branch is created, you can freely switch between both branches at any time by using the Current branch dropdown.

image showing the current branch button

Do note, however, that if you have outstanding, uncommited changes when you switch branches, you will be asked if you want to bring the changes to the branch you are switching to, or to leave the changes in your previous branch.

Any commits that you make in the new branch will not apply to the previous branch.

c. Pushing and publishing commits

The first time you make a branch, the option to push your commits onto the repository will not be available. Instead, you will be prompted to Publish branch.

github desktop publish branch

This option is no different from a push. It simply creates the branch in the remote repository (i.e. on GitHub) as well, so that there is a place for GitHub to push your changes.

d. Multiple branches and branch history

You can create as many branches as you want in your repository, from any branch in your repository. Once you have created a branch, the next time you do so, you can choose to branch from any existing branch that currently have:

create multiple branches under main branch or another branch

New branches that you create will inherit the entire history of the branch that you are creating it from.

github desktop new branch created inherited entire history of the branch that it is created from

e. Updating the main branch

While you are working on your new branch, you can still commit and push onto your main branch.

can still commit and push onto main branch even there is a change in other branch that is not merged

This is not ideal, however, because doing so means that when you merge the branches together later, you may run into a merge conflict. The merge conflict is not guaranteed to happen—if the differences are small, Git will be able to automatically resolve any conflicts for you.

When branching, don’t be afraid to update the main branch if necessary. Sometimes, you may need to deploy a bug fix on the main branch while your new feature branch is being developed.

3. Merging branches

Once you’re happy with the work you have done in your branch, and are ready to apply the changes back to your project, you will need to merge your branches.

There are 2 ways to do so:

a. Pull request on GitHub

The easier of the 2 ways is to start a pull request on GitHub. To do so, create a pull request from your branch:

click on create pull request in github desktop

This will lead you to the repository on the GitHub website and bring you to the Open a pull request page.

Ensure that the base branch (where you want to merge into) and the compare branch (where the changes are from) are correct. Then, fill in the title and description fields and click on Create pull request.

opened github browser rename and add description for team or straight away click on create pull request

GitHub will show you one of 2 things, either:

  1. This branch has no conflicts with the base branch, which is the best case scenario. If you get this, rejoice and proceed to click on Merge pull request and complete the pull request.
Merge pull request
  1. This branch has conflicts that must be resolved. If you get this, you will need to resolve the merge conflict between the branches.

Once the pull request has been done, you will need to go back to GitHub Desktop, and click on Fetch origin, followed by Pull origin to see the newly-merged branch in your computer.

A screenshot of the github desktop topbar with the fetch origin button highlighted.
Once you click on it, the button will change to Pull origin, and you’ll need to click on it again.

To check that the merge has been properly applied, you can go to the History tab in your main branch to verify that the branched commits are now in there, together with the pull request commit.

github desktop check history of main branch after successfully pulled

b. Command line git merge

If you’d like to merge the branches without the extra “Merge pull request” commit, you can also merge your branches through the Git command line. However, that requires that you have Git installed on your computer. If you don’t have it, you’ll first need to download and install it.

Once done, open your project folder by right-clicking on the repository dropdown, then selecting Show in Explorer (or Show in Finder for macOS):

github desktop open file in explorer

Then, right-click and select Git Bash Here (you’ll need to install the Git command line to get this):

unity folder open git bash here

This will open up the Git command line, which will allow you to do many of the same things that GitHub Desktop allows you to do, plus many more things that GitHub Desktop is unable to do.

To begin the merge, you’ll need to first go to the branch you want to merge into (in our case, the main branch):

git checkout main

As a best practice, it is always best to do a pull from origin before you merge, in case there are any incoming changes. Otherwise, if there are pulls after you merge, you’ll need to do another pull request.

git pull origin main

To do the actual merge:

git merge your-branch-name
git merging process

If there are no merge conflicts, the commits from the branch will be added into your main branch (or any other branch you choose to merge into):

github desktop merge completed

Otherwise, Git will tell you that “Automatic merge failed; fix conflicts and then commit the result”, and create a merge conflict. If that happens, you can open up GitHub Desktop to see the merge conflicts, and proceed to resolve them.

c. Deleting your branch

Once you are done merging your branch, you are recommended to delete it to prevent clutter. Otherwise, things can get messy really fast if you branch a lot!

github browser delete branch

4. Conclusion

In game development, branching is a very important feature that developers are strongly recommended to make use of. You’ll never know when a new piece of code you are adding will break your entire codebase and cause your game to become unplayable.

Branching helps to ensure that you always have a stable, playable build of your game ready for presentation to prospects, playtesters or your existing clients.