Chapter 25: Git GitHub Branch

Git branches (and how they appear / behave on GitHub)

This is the topic that makes people go from “I can commit” to “I can actually work like a real developer in a team”.

I’m going to explain it very slowly and clearly, like we’re sitting together looking at your laptop screen + GitHub page at the same time — with real commands, real outputs, screenshots-like descriptions, analogies, and a complete example you can follow right now.

1. What is a branch in Git? (super simple mental model)

A branch is a movable label / pointer that points to one specific commit in your history.

  • When you make a new commit → the branch pointer automatically moves forward to the new commit
  • The old commits stay exactly where they were
  • You can have many branches pointing to different commits at the same time

Real-life analogy most people love:

Your project is a story book.

  • main branch = the official published version everyone reads (stable, production)
  • feature/add-login branch = you made a photocopy of the current book, went to a quiet room, wrote a new chapter (login page), while the original book stayed untouched
  • When the new chapter is good → you staple it into the official book (merge)
  • If it’s bad → you throw your photocopy away — official book unchanged

Key facts in 2026:

  • The default branch name is now main (not master — GitHub, GitLab, most tools changed this years ago)
  • Branches are extremely cheap — creating one takes milliseconds and almost no disk space
  • Branches exist locally on your laptop + can be pushed to GitHub so your team sees them

2. Branches on GitHub — what you actually see

On github.com in your repository:

  • Branches tab → list of all branches that have been pushed (not just local ones)
  • main is usually highlighted as the default
  • Each branch name is clickable → shows the code/files as they look at the tip of that branch
  • Pull Requests almost always start from a branch → “Compare changes across two branches”

3. Hands-on Example — Let’s Create, Use, Push, and Merge a Branch

We’ll do this from scratch so you can copy-paste everything.

Step 1 – Start with a small local repo (or use existing one)

Bash

Step 2 – See current branches

Bash

You see:

text

The * means “you are currently on this branch” (HEAD is here)

Step 3 – Create and switch to a new branch

Two modern ways (both correct in 2026):

Bash

Now:

Bash
text

You’re now safely working on a parallel timeline.

Step 4 – Make changes on the new branch

Edit README.md (add lines):

text

Then:

Bash

Now history has diverged:

Bash

Something like:

text

Step 5 – Push the branch to GitHub (so team can see it)

First time for this branch:

Bash

You see:

text

Now go to github.com/your-username/todo-app-branches → Branches tab → You see main and feature/add-todos

Click feature/add-todos → you see README.md with the three new todos!

Step 6 – Merge back to main (when feature is ready)

Bash

Clean fast-forward output:

text

Now graph:

Bash
text

Both branches now point to the same commit — clean!

Step 7 – Clean up

Bash

Or on GitHub → Branches tab → trash icon next to the branch.

4. Everyday Branch Commands Cheat Sheet (2026)

What you want to do Command (modern preferred) Classic alternative Notes
List local branches git branch * = current
List all (local + remote) git branch -a -r = only remote
Create + switch git switch -c feat/add-login git checkout -b feat/add-login Use switch — safer
Switch branch git switch main git checkout main
Push new branch first time git push -u origin feat/add-login -u sets tracking
Delete local branch git branch -d feat/old -D to force
Delete remote branch git push origin –delete feat/old git push origin :feat/old
See tracking info git branch -vv Shows which remote branch each tracks

5. Quick Summary – Branches on GitHub vs Local

  • Local branch → only exists on your laptop until pushed
  • Remote branch (origin/feat/add-login) → appears on GitHub after git push
  • Tracking → local branch remembers its remote counterpart (set with -u)
  • Pull Request → almost always created from a pushed branch to main

Got the branch feeling now? Branches = cheap, safe parallel universes for your code — and GitHub is just the place where you share those universes with your team.

Next?

  • Want to do a full feature branch → PR → merge cycle live?
  • How to handle merge conflicts when branches diverge?
  • Or remote tracking branches explained deeper?

Just tell me — we’ll keep going step by step. You’re doing awesome! 🚀

You may also like...

Leave a Reply

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