Chapter 12: Git Branch

Git Branch — the single most powerful and most-used feature after committing.

If commits are “save points,” branches are parallel universes where you can experiment safely without breaking the main story. Almost every real-world Git workflow (GitHub Flow, GitFlow, trunk-based, feature branches) revolves around them.

I’m going to explain it like we’re building a small todo app together — step by step, with real commands, outputs, analogies, and why branches exist.

1. What is a Git Branch? (Super simple analogy)

Imagine your project is a storybook:

  • The main branch (or main in 2026) = the official published book everyone reads. Stable, production-ready.
  • A branch = you make a photocopy of the current book, go to a quiet room, scribble wild ideas (new chapters, rewrite ending), while the original book stays untouched.
  • Finished & good? → Merge your copy back into the main book.
  • Bad idea? → Throw your copy away — main book unaffected.

Technically:

  • A branch is just a lightweight movable pointer to a specific commit.
  • When you create a branch, Git doesn’t copy files — it just says “from this commit onward, call this timeline ‘feature/dark-mode'”.
  • HEAD (your current position) points to the tip of your current branch.
  • Every new commit on a branch moves that branch pointer forward (main stays put until you merge).

2. Why Branches? (Real 2026 reasons)

  • Work on new features / bug fixes without breaking production
  • Multiple people / tasks at once (you on login, teammate on payment)
  • Experiment safely (try risky refactor, throw away if fails)
  • Isolate changes → easier code review via Pull Requests
  • Maintain old versions (hotfix on v1.2 while main is v2.0)
  • GitHub / GitLab / Bitbucket all build Pull Requests, Actions, Reviews around branches

In 2026, default branch is almost always main (Git has shifted fully from master since ~2020–2023; new repos use main out of the box).

3. Hands-on: Let’s Create & Use Branches Right Now

Start fresh or continue previous demo:

Bash

Check branches:

Bash

Output:

text

→ * shows current branch (HEAD is here)

Create a new branch (two common ways):

Way A – Create without switching:

Bash

Way B – Create and switch (most people prefer this – one-liner):

Bash

Now:

Bash
text

No — after checkout -b or switch -c:

text

You’re now on the new branch — safe to experiment.

Make changes:

Bash

Now history diverges:

Bash

Something like:

text

→ main pointer stayed at old commit → feature/add-tasks moved forward

4. Switch Between Branches

Bash

→ Your files change back! README.md loses the new todos (because they live only on the other branch)

Bash

Switch back:

Bash

5. Merge Branch Back to Main (The Happy Ending)

When feature is ready:

Bash

Output (fast-forward merge – clean):

text

Now:

Bash
text

→ Both pointers now at same commit (merged)

Clean up (optional but good):

Bash

6. Branch Commands Cheat Sheet (Daily 2026 Use)

Action Command (preferred) Old / Alternative Notes
List local branches git branch * = current
List all (local + remote) git branch -a -r for remote only
Create branch (no switch) git branch feature/new-ui Rarely used alone
Create + switch git switch -c feature/new-ui git checkout -b feature/new-ui Modern Git recommends switch
Switch branch git switch main git checkout main switch safer (won’t mess with files)
Merge into current branch git merge feature/new-ui Run from target branch (usually main)
Delete branch (safe) git branch -d feature/old Fails if not merged
Force delete git branch -D feature/bad-experiment Use when you don’t care
Rename current branch git branch -m feature/better-name Careful if pushed already
See last commit on each branch git branch -v Shows hash + message

7. Quick Real-World Flow (What Most Teams Do in 2026)

  1. git checkout main && git pull → fresh main
  2. git switch -c feature/user-profile
  3. Work → add → commit (many times)
  4. git push -u origin feature/user-profile → share
  5. Create Pull Request on GitHub → review → merge
  6. git checkout main && git pull → update local main
  7. git branch -d feature/user-profile → cleanup

Got the branch feeling? Branches = cheap, fast, safe parallel timelines.

Next?

  • Merge conflicts hands-on?
  • Pull Requests with branches?
  • Rebase vs merge?
  • Remote branches & tracking?

Just say — we’ll continue the class. You’re doing awesome! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *