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)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
mkdir todo-app-branches cd todo-app-branches git init echo "# Todo App 2026" > README.md git add README.md git commit -m "Initial commit: basic README" |
Step 2 – See current branches
|
0 1 2 3 4 5 6 7 8 |
git branch # or more info: git branch -v |
You see:
|
0 1 2 3 4 5 6 |
* main abc1234 Initial commit: basic README |
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):
|
0 1 2 3 4 5 6 7 8 9 10 |
# Way 1 – recommended (Git 2.23+) git switch -c feature/add-todos # Way 2 – classic way (still works everywhere) # git checkout -b feature/add-todos |
Now:
|
0 1 2 3 4 5 6 |
git branch |
|
0 1 2 3 4 5 6 7 |
main * feature/add-todos |
You’re now safely working on a parallel timeline.
Step 4 – Make changes on the new branch
Edit README.md (add lines):
|
0 1 2 3 4 5 6 7 8 |
- Buy milk - Call mom - Finish Git branch lesson |
Then:
|
0 1 2 3 4 5 6 7 |
git add README.md git commit -m "Add three initial todo items" |
Now history has diverged:
|
0 1 2 3 4 5 6 |
git lg # or git log --oneline --graph --decorate --all |
Something like:
|
0 1 2 3 4 5 6 7 |
* def5678 (HEAD -> feature/add-todos) Add three initial todo items * abc1234 (main) Initial commit: basic README |
Step 5 – Push the branch to GitHub (so team can see it)
First time for this branch:
|
0 1 2 3 4 5 6 |
git push -u origin feature/add-todos |
You see:
|
0 1 2 3 4 5 6 7 8 9 |
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:Webliance/todo-app-branches.git * [new branch] feature/add-todos -> feature/add-todos Branch 'feature/add-todos' set up to track remote branch 'feature/add-todos' from 'origin'. |
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)
|
0 1 2 3 4 5 6 7 |
git switch main git merge feature/add-todos |
Clean fast-forward output:
|
0 1 2 3 4 5 6 7 8 9 |
Updating abc1234..def5678 Fast-forward README.md | 3 +++ 1 file changed, 3 insertions(+) |
Now graph:
|
0 1 2 3 4 5 6 |
git lg |
|
0 1 2 3 4 5 6 7 |
* def5678 (HEAD -> main, feature/add-todos) Add three initial todo items * abc1234 Initial commit: basic README |
Both branches now point to the same commit — clean!
Step 7 – Clean up
|
0 1 2 3 4 5 6 7 |
git branch -d feature/add-todos # local delete git push origin --delete feature/add-todos # remote delete |
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! 🚀
