Last updated on 6th March 2026

Branching and Merging

If you're working on a new feature, or pushing a bug fix to your site, branching is a great way to ensure you don't cause any issues with your main version.

In this guide we will cover the concepts of branching and merging; using Git as our version control system in the examples covered.

Introduction

In this guide we are going to cover 3 commands - branch, checkout and merge. A branch is essentially a version of your app that can work on, for example a development or bug fix branch. Checkout is the process of switching from one branch to another, so you don't make changes to the wrong version of your site. Finally, merge brings two different branches into one, effectively creating a single version of your site from two different versions.

Let's now look at using these commands in the context of maintaining and improving our site.

Creating a Branch

Our website is live and online, but we would like to add a new feature, let's say a shopping cart so our customers can start buying our product.

We will create a new branch called cart:

$ git checkout -b cart
Switched to a new branch 'cart'

This creates a new branch called cart and automatically switches to it, ready to start working on. That command is shorthand for the following:

$ git branch cart
$ git checkout cart

So now we have two branches, our main branch, referred to as master, and our newly created cart branch.

Another Branch

We are now working in our cart branch, but let's say we have found a bug on our main site. At this point, we should create a new branch:

$ git checkout -b bugfix
Switched to a new branch 'bugfix'

We can work on our fix without disturbing the site, and commit it to our bugfix branch:

$ git commit -m "fixed the bug"
[bugfix c42b77e] fixed bug
1 file changed

And now push the branch to our remote repository:

$ git push -u origin bugfix
* [new branch]    bugfix -> bugfix
Branch bugfix set up to track remote branch bugfix from origin

Merging a Branch

We have tested the fix and we are happy, so let's merge this change into master. But first, we need to switch back to our master branch:

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'

Now we can merge our bugfix:

$ git merge bugfix
Updating 68fb3f6..c42b77e
Fast-forward
index.html | 1
1 file changed

And push to GitHub:

$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
to git@github.com:adamw/first-project/repository.git
68fb3f6..c42b77e  master -> master

Housekeeping

Everything looks good, and the bug is now fixed and deployed. Let's remove our bugfix branch:

$ git branch -d bugfix
Deleted branch bugfix (was c42b77e)

We will want to remove the branch from our remote repository as well.

$ git push origin --delete bugfix
To git@github.com:adamw/first-project/repository.git
- [deleted]      bugfix

Summary

In this guide, we have covered creating, merging, and removing branches, both locally and remotely.

Creating a branch from a specific starting point

By default, git branch <name> uses your current branch as the starting point for the new branch. If you want the new branch to start from a different branch or tag, pass it as a second argument:

$ git branch <name> <branch|tag>

For example, if you're currently on a bugfix branch but want to create a new feature branch based on master, you can skip switching branches first:

$ git branch feature master

This creates the feature branch from master without leaving your current branch. You can then switch to it with git checkout when you're ready.

Safe vs force branch deletion

The Housekeeping section above uses git branch -d to delete a branch. The lowercase -d flag is the safe option -- it only deletes the branch if it has been fully merged with its corresponding remote branch (or with HEAD if no remote branch has been set). If the branch contains unmerged work, Git will refuse and warn you.

If you're certain you want to discard the branch and its unmerged commits, use the uppercase -D flag to force the deletion:

$ git branch -D <branch>

Note: Both -d and -D only delete the branch in your local repository. To remove a branch from the remote, use git push origin --delete <branch> as shown in the Housekeeping section.

If you need to rename an existing branch instead of deleting it, see how to rename a branch in Git.


Once your branches are merged and your code is ready, DeployHQ can deploy it to your servers automatically from any Git repository.