Skip to main content
Git Basics
CHAPTER 04

Git Branching Fundamentals

Updated: May 15, 2026
20 min read

# CHAPTER 4

Git Branching Fundamentals

1. Introduction

So far, our Git repository is a single, straight line of history. We make a commit, and then another commit on top of it. But in software development, creating experimental features directly on the main, working codebase is incredibly dangerous. What if your new code breaks the website? This is where Branching comes in. Branching is the most powerful feature of Git. It allows you to create a completely isolated, parallel universe of your project. You can build new features or break things in this alternate universe without ever affecting the stable, main version of your code.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the conceptual purpose of a Git Branch.
  • Identify the default main (or master) branch.
  • Create a new feature branch (git branch).
  • Switch between different branches (git checkout / git switch).
  • Understand how files dynamically change on your hard drive when switching branches.

3. Beginner Explanation

Imagine you are writing a Sci-Fi novel.
  • The Main Branch: This is the official, published version of your book. It is perfect.
  • The Branch (Parallel Universe): You have a crazy idea: "What if I kill off the main character?" You don't want to delete the main character in your official book, because you might change your mind. So, you make a magical photocopy of the entire book (a Branch).
  • You name this photocopy experiment-death. You write 50 pages of the character dying.
  • If you hate it, you just throw the photocopy away. Your original book is perfectly safe. If you love it, you can "Merge" those pages back into the official book.

4. The main Branch

When you run git init, Git automatically creates a default branch. Historically, this was called master. Today, the industry standard is to call it main. Golden Rule: The main branch should ONLY contain stable, working, production-ready code. You should rarely write code directly on main.

5. Creating and Switching Branches

To see a list of all your branches, type:
bash
1
git branch

*(The branch with an asterisk * next to it is the one you are currently on).*

To create a new branch:

bash
1
git branch feature-login

To switch to that new branch:

bash
123
git checkout feature-login
# OR (in newer versions of Git)
git switch feature-login

The Pro-Shortcut: You can create a new branch and instantly switch to it in one single command using the -b flag:

bash
1
git checkout -b feature-shopping-cart

6. Mini Project: Build Feature Branch Workflow

Let's see branching magic in action.

Step-by-Step Walkthrough:

  1. 1. Ensure you are on the main branch (git checkout main).
  1. 2. Make sure your index.html has the text "Hello World".
  1. 3. Create and switch to a new branch: git checkout -b feature-dark-mode
  1. 4. Open index.html in your text editor. Change the text to "Hello Dark Mode".
  1. 5. Save, Stage, and Commit:
``bash git add . git commit -m "Add dark mode text" `
  1. 6. The Magic: Type git checkout main. Instantly open index.html in your editor. The text has magically reverted to "Hello World"! Git physically swapped the files on your hard drive to match the main timeline.
  1. 7. Type git checkout feature-dark-mode. Open the file again. The text changes back to "Hello Dark Mode".

7. Real-World Scenarios

A web agency has a live client website running on the
main branch. The client asks for a massive redesign of the homepage. The developer creates a branch called v2-redesign. They spend three weeks writing code, breaking layouts, and testing new images on this branch. During week two, the client calls: "There is a typo on the live 'About Us' page! Fix it now!" Because the developer used branching, they simply type git checkout main (instantly returning their computer to the stable live code), fix the typo, deploy it, and then type git checkout v2-redesign to seamlessly return to their messy, half-finished redesign work.

8. Best Practices

  • Descriptive Naming: Don't name your branches branch1 or test. Name them based on the specific feature or bug you are fixing.
  • Good: bugfix/header-typo
  • Good: feature/user-authentication

9. Common Mistakes

  • Uncommitted Changes Before Switching: If you edit files on main, forget to commit them, and then type git checkout feature-login, Git will carry those uncommitted messy changes over to the new branch with you. Always run git status to ensure your "working tree is clean" before switching branches.

10. Exercises

  1. 1. What is the fundamental purpose of the main (or master) branch in a software project?
  1. 2. What command allows you to create a new branch and switch to it simultaneously?

11. FAQs

Q: How many branches can I have? A: Infinite. Branches in Git are incredibly lightweight. They don't take up megabytes of disk space like physically copying a folder would. You can have hundreds of branches in a repository without any performance issues.

12. Summary

In Chapter 4, we unlocked the true power of Git by mastering Branching. We learned that branches are isolated, parallel environments that allow developers to experiment fearlessly without jeopardizing the stability of the
main production codebase. We executed the core workflow of creating branches (git branch) and traversing timelines (git checkout), physically observing how Git seamlessly manipulates the files on our hard drive to match our selected reality.

13. Next Chapter Recommendation

We have successfully isolated our experimental feature. But once the feature is perfect, how do we get that code back into the official
main` branch? Proceed to Chapter 5: Git Merge and Conflict Resolution.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·