The Git-ing Tree: Git Branching and Merging Strategies
Tags: git
Published: 2021-03-17 11:59:28

---
title: "The Git-ing Tree: Git Branching and Merging Strategies"
published: true
description: All about git branches and merging.
tags: git
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqk46ximn2y5xh5ur5wb.png
canonical_url: https://cctplus.xyz/2021/03/17/giting-tree
---
Hey yall! Let’s grab a cup of coffee or whatever your beverage of choice is and talk about git branches and merging.

{% youtube JGzKN8m3Ipc %}

# What is a branch

A branch is the area where edits and commits are stored. If you watched my [last video](https://youtu.be/4KwLqLXGPn8) about git remote servers, then I was actually pushing to a branch already, the `main` branch.

By default, a git repo is on a master or a main branch. There has been a push for repos to have a main branch, but that's a topic for a different video or maybe even just a blog post. Just know, I’ll be using `main` in the rest of this video.

Branches can be created whenever you need one, which we will talk about in just a minute. Creating a branch will give you a new area to work in that doesn’t adjust your main branch.

When a branch is created, you create a fork in your git repo. This is what a repo looks like with commits.

![image](https://res.craft.do/user/full/e08d0ba1-ee73-de35-2f90-2e261c097071/doc/82149C1E-E8FF-4BBE-8EA8-FE551C5A7E68/760B3011-F445-4B58-B63B-F1065CD12421_2)

And this is what it looks like when you create a branch

![image](https://res.craft.do/user/full/e08d0ba1-ee73-de35-2f90-2e261c097071/doc/82149C1E-E8FF-4BBE-8EA8-FE551C5A7E68/58576EDD-C0EC-4F0D-BDB7-8F602A4E7DD5_2)

# Why branch your code?

There are two main reasons to create branch are

1. Keep changes isolated from your main code base
2. Work on different modules at the same time

Keeping changes isolated from your main code is the main benefit here if you are a solo developer. This allows you to keep potentially breaking code away from working code.

Working on different modules at the same time is really useful for teams, so that someone can work on feature A while another person works on feature B and someone else can fix issue number 512. All 3 can have their own branch and don’t have to worry about breaking changes, until bringing the code back together.

# Creating a branch

To create a branch in the code, you need to run a command

 `git branch <name of branch>`

Now to start working on that branch, you need to run a checkout command. The checkout command is used to switch between versions of the repo and by specifying the branch name, then git will grab the branch

`git checkout <name of branch>`

Once you’ve checked out the branch, you can now make changes on that branch just like normal.

```shell
git add <file name>
git commit -m <Message>
git push
```

# How to get the code back together

Now, the code has been split into branches, what happens when you want to get the branches back to s ingle point. That’s where the `merge` command comes into play.

To merge the branch back to main run the following commands.

```shell
git checkout main
git merge <branch name>
# optional - delete the old branch now that it is in main
git branch -d <branch name>
```

You might run into conflicts, but those can be resolved in your text editor. Once you resolve the conflicts, you can add, commit, and push your resolution like any other change.

# My Use Case of Branches


I use branches for each release of my apps. You can see that with iHog’s GitHub Repo. There is a `master` branch and then the version I'm currently working on, `2021-2`

![image](https://res.craft.do/user/full/e08d0ba1-ee73-de35-2f90-2e261c097071/doc/82149C1E-E8FF-4BBE-8EA8-FE551C5A7E68/5BC54927-397D-4C92-B9E4-F344DE30CE6B_2)
> Master or Main Branch is always the latest release


I keep master or main as a release branch. Meaning, that it’s not in development. This is the code used that is currently on the app store.

If I’m adding a feature in the middle of a version, then I will add a branch for that feature off of the version’s branch and then merge the feature back into the version branch.

This work flow allows me to issue bug fixes without needing to finish the feature if need be.

----

Thanks for reading! I appreciate it. If you found this useful, then please make sure to share this wherever you can!