DEV Community

Cover image for The Git-ing Tree: Git Branching and Merging Strategies
Maegan Wilson
Maegan Wilson

Posted on • Originally published at

The Git-ing Tree: Git Branching and Merging Strategies

Hey yall! Let’s grab a cup of coffee or whatever your beverage of choice is and talk about git branches and merging.

What is a branch

A branch is the area where edits and commits are stored. If you watched my last video 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.


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


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.

git add <file name>
git commit -m <Message>
git push
Enter fullscreen mode Exit fullscreen mode

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.

git checkout main
git merge <branch name>
# optional - delete the old branch now that it is in main
git branch -d <branch name>
Enter fullscreen mode Exit fullscreen mode

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


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!

Top comments (0)