DaleSchool

Working Safely with Branches

Beginner15min

Learning Objectives

  • Explain what branches are and why they are needed
  • Create and switch branches on the GitHub web interface
  • Work on a branch and merge it into main

Why Branches Are Needed

What happens if something goes wrong while developing a new feature? If you worked directly on main, the original is broken.

Branches solve this. Keep the original (main) stable at all times and do new work on a separate copy (branch). If you are happy with the result, merge it. If not, just discard it.

main    ─────────────────────────────── (always stable)
              \                   /
feature        ──── work ──── done   (experimental)

Creating a Branch

Here is how to create a branch on the GitHub web interface.

  1. On the repository main page, click the branch selector above the file list (default: main)
  2. Type a new branch name in the text box: add-profile
  3. Click Create branch: add-profile from 'main'

Branch naming conventions:

  • Use lowercase and hyphens (-)
  • Make the name describe what the branch does: fix-typo, add-dark-mode, update-readme

Editing Files on a Branch

After creating a branch, any changes you make are saved only to that branch.

  1. Confirm you are on the add-profile branch (the branch selector shows add-profile)
  2. Click README.md → click the pencil icon
  3. Add content:
## About Me

- Name: John Doe
- Interests: coding, reading, cooking
- Goal: collaborate using GitHub
  1. Commit changes → message: docs: add profile info
  2. Select Commit directly to the add-profile branch

Comparing Branches

Check how main and add-profile differ.

  1. Switch the branch selector back to main
  2. Look at README.md — your added content is not there
  3. Switch back to add-profile — the content is there

Branches are independent of each other.

Merging a Branch

Once your work is done, merge it into main. The next lesson covers this in detail with Pull Requests. For now, here is the quick approach.

  1. Click the Pull requests tab at the top of the repository
  2. Click New pull request
  3. Confirm the settings: base: main ← compare: add-profile
  4. Preview the diff
  5. Create pull request → enter a message → Merge pull request

After merging, your changes are reflected in main.

"Why?" — Why Branches Are Essential for Teams

Working alone, you might not need branches. But on a team, they are essential.

While teammate A develops the login feature, teammate B works on the payment feature. If both commit directly to main, their code will conflict.

With branches:

  • A works on the feature/login branch
  • B works on the feature/payment branch
  • Each finishes, gets reviewed, and merges into main

Parallel development without conflicts.

Deep Dive

Deleting a Branch

Clean up branches after they are merged. Accumulated stale branches make management harder.

  1. After a Pull Request is merged, a Delete branch button appears
  2. Or go to the Code tab → N branches link → click the trash icon next to the branch

Deleting a branch does not remove its commit history. You can restore it at any time.

Default Branch Name — main vs master

The default branch used to be named master. Since 2020, GitHub changed the default for new repositories to main.

You may see master in older repositories or tutorials. The functionality is identical — only the name differs.

You can change the name in Settings → Default branch.

  1. Create an add-profile branch in my-first-repo.
  2. Edit README.md on that branch and commit.
  3. Switch back to the main branch and confirm the changes are not there.

Q1. What is the main reason for using branches?

  • A) To share a repository across multiple accounts
  • B) To work on new changes safely without touching the original (main)
  • C) To upload files faster
  • D) To auto-generate commit messages

Further Reading