Creating GitHub Branches

How to create and merge GitHub branches using the GitHub website or your command line/terminal.

In my software engineering bootcamp, creating and merging branches was the bane of my existence. If you’re just starting out in full stack or web development, I’m sure you’re also all too familiar with the misery of making branches, not quite knowing how to work on them or merge them to your main branch. Perhaps you’ve even tried copying and pasting a myriad of code to your main branch manually. Ugh! I’ve been there. Here is a quick and simple explainer of how to do so from either the GitHub website, or directly from the command line in your code editor.

Sometimes interacting with GitHub can seem as obscenely confusing as their logo. Is it a squid? Is it a cat? Is it a small child very confused about their Halloween costume choice?

GitHub Branches are crucial in writing code and ensuring that only the features that work are integrated to your main project file. Without branches, a new feature and its code might introduce a multitude of bugs into hours of previously-functioning hard work. Branches are especially important when working with a team of people, all manipulating different parts of the code in various capacities.

A visual representation of GitHub branches

Note that GitHub has officially changed the naming of the “Master” branch, the official branch of code where all feature branches are eventually merged once working, to the “Main” branch.

Using the GitHub Website

When on a given repository page, you’ll see a dropdown menu, and the number of branches that exist listed next to it:

In my case, I am on the main branch, the only branch that currently exists.

Select the “main” drop down menu and begin typing to either locate or create a new branch with that name:

Once you select “Create branch: new-feature,” GitHub will direct you to the page for the repository’s new branch:

Et voilà! Here is your new branch. However, you will still have to use your command line to switch to this new branch in your code editor to continue working on it, which I’ll cover below.

Using the Command Line (Terminal)

To make a new branch solely using the command line in your code editor, you’ll use

git branch <new-branch-name> like so:

To switch to the new branch you’ll use

git checkout <new-branch-name>

(*Note: this is the step you will have to take in the command line if you’ve created a branch from the GitHub website.)

To make things easier, and to ensure you switch to the new branch before working on your feature, you can combine these two previous commands with

git branch -b <new-branch-name>

Thanks for reading, and join me next week where I’ll cover merging GitHub branches!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam Hall

Sam Hall

Full stack developer and stand-up comic.