Lesson 24 of 51 ~25 min
Course progress
0%

Branching Basics – Create, Switch, Merge

Master Git's most powerful feature: branching. Learn to create branches for parallel development, switch between them seamlessly, and merge changes back.

Branches are Git’s superpower. In other systems, branches feel heavy, bureaucratic, even dangerous. In Git, branches are lightweight pointers. They are so cheap you can create them like sticky notes, throw them away, and create new ones without consequence. This chapter explores why branches are revolutionary, how they work, and how to use them daily without fear.


The Essence of a Branch

A branch is not a copy of your code. It’s just a pointer to a commit. When you commit on that branch, the pointer moves forward. That’s it.

Visualization:

gitGraph
  commit id: "A"
  commit id: "B"
  branch featureX
  checkout featureX
  commit id: "C"

Here, featureX is just a label pointing to commit C. The data underneath is shared.


Creating Branches

Create a branch:

git branch feature/login

Switch to it:

git checkout feature/login
# or modern syntax
git switch feature/login

Create and switch in one step:

git checkout -b feature/login

Branches are instant—no duplication, no server roundtrip.


Listing and Deleting Branches

List local branches:

git branch

Delete a branch:

git branch -d feature/login

Force delete if unmerged:

git branch -D feature/login

Remote Branches

Branches can exist remotely too. Push a branch:

git push -u origin feature/login

Now teammates can fetch and check it out.

Visualization:

flowchart LR
  Local[Local Branch feature/login] -->|push| Remote[Remote Branch feature/login]

Why Branches Are Cheap

In older VCS, branching required copying entire directories. In Git, a branch is a 41-byte file containing the hash of the commit. That’s it. Storage is negligible, creation is instant.

cat .git/refs/heads/feature/login
# shows a SHA-1 commit hash

That simplicity explains Git’s branching revolution.


Branching Workflows

Solo Developer

You want to test a new algorithm:

git checkout -b experiment/alt-sorting
# hack hack hack
git checkout main
git branch -D experiment/alt-sorting

Experiment guilt-free. No clutter in history.

Team Developer

On a team, branches isolate features and fixes. Each developer works independently, then merges into main via pull requests. This parallelism accelerates development.

Visualization of multiple branches:

gitGraph
  commit id: "Init"
  branch featureA
  checkout featureA
  commit id: "A1"
  commit id: "A2"
  checkout main
  branch featureB
  checkout featureB
  commit id: "B1"
  checkout main
  merge featureA
  merge featureB

Naming Conventions

Good branch names communicate purpose:

  • feature/login-page
  • bugfix/crash-on-startup
  • hotfix/security-patch
  • experiment/new-ui

Avoid vague names like newstuff or temp.


Branching Strategies

Branching enables strategies like GitHub Flow, Git Flow, and trunk-based development. Each balances speed and stability differently, but all rely on the cheapness of branches.


Think Different Mindset

Branches are not bureaucratic red tape—they are invitations to experiment. Git teaches us that divergence is natural, even desirable. Parallel universes aren’t mistakes; they’re opportunities. With branches, you stop fearing “breaking main” and start exploring boldly. History isn’t a straight line—it’s a tree. And trees grow stronger with many branches.


Branches are cheap, fast, and liberating. They allow solo developers to experiment fearlessly and teams to collaborate in parallel. In the next chapter, we’ll go deeper into merges and see how those branches reconcile into a shared history.