Episode 3 of 7GitHub for Beginners

Branches

The video only loads when you click the picture. Until then your browser does not connect to YouTube.

Trying something without breaking the site

Say your website works and people visit it. Now you want to try a new design for the home page. If you work straight on the live version, then while the design is half done, your site is half broken.

A branch is a copy off to the side

A branch is a copy of the project that you work on off to the side. The main line carries on as it is, and you split off from it.

The main line is called main. It's the official version. Every branch starts from main at a certain point and then gets commits of its own. Meanwhile nothing happens on main. On your branch you can try things, break things and fix them.

Merge it, or just leave it

Did it work out? Join the branch back into main. That's called a merge: everything from the branch goes into the main line, and main carries on from there.

Didn't work out? Leave the branch. Main is untouched, because you never touched it.

Several branches at once

You can have several branches at the same time: one for the new design, one for a bug fix, one for a page you haven't decided on. Each lives on its own until you merge it.

Create a branch on the website

  1. On the repo page, at the top left of the file list, click the button that says main.
  2. In the box that opens, type a name for the new branch. Short, and saying what it's for, like new-homepage.
  3. Click the line Create branch new-homepage from main.
The branch menu on a repo page: the main button, the name new-homepage typed in, and the line “Create branch new-homepage from main”.123
  1. Click the button that says main.
  2. Type the name of your new branch.
  3. Click Create branch … from main.

Where your commits go now

The button doesn't say main anymore. It shows your branch name. Every commit you make from now on goes to this branch, not to main.

The repo page after switching: the branch button now reads new-homepage, next to “2 Branches”.1
  1. The button shows the branch you're on: new-homepage.

Change something in the README and commit it. Then switch back to main with the same button. The change isn't there. It's waiting on your branch.