Branches

Summary

Branches let you work on changes without treating every experiment as part of the main line of history. The practical idea is simple: a branch is another line of development that points at commits, so you can isolate work, test changes, and merge them later when they are ready.

Git branch and history diagram from the official Git Book

Official Git diagram showing how branch pointers and commit history relate to each other.

Why this matters

  • branches are part of normal team and solo Git workflow
  • they reduce risk by isolating unfinished work from the main branch
  • if you do not understand branches, merge work, pull requests, and collaboration quickly become confusing

Environment / Scope

ItemValue
TopicGit branch model
Best use for this noteunderstanding safe parallel work in Git
Main focuscreate, switch, inspect, merge mentally
Safe to practise?yes, in a throwaway repo

Key concepts

  • Branch - a named pointer to a line of commits
  • Main branch - the primary line of history, often main
  • Feature branch - a temporary branch for one task or change
  • Merge - bringing changes from one branch into another
  • Checkout / switch - moving your working context to another branch

Mental model

Think about branches like this:

main
  \
   feature-branch

Both branches share history until they diverge.

This means:

  1. you create a branch from the current state
  2. you work there without touching main
  3. you merge it back when the work is ready

Everyday workflow

StepTypical commandMeaning
See branchesgit branchlist local branches
Create and switchgit switch -c feature-xmake a new branch and move to it
Check stategit statussee current branch and changes
Switch backgit switch mainreturn to main branch
Merge workgit merge feature-xbring feature branch commits into current branch

Common misunderstandings

MisunderstandingBetter explanation
”A branch is a full copy of the repo”it is mainly a movable pointer to commits, not a giant duplicate
”Branches are only for teams”they are useful even when working alone
”Merging is the same as pushing”merge changes local history; push sends commits to the remote
”Main should be where I experiment”experiments are usually safer in feature branches

Verification

CheckExpected result
git branchshows current local branches
git statusshows current branch clearly
git log --oneline --graph --allshows branch history visually

Pitfalls / Troubleshooting

ProblemLikely causeWhat to check
Changes appear on the wrong branchwork started before switching branchesgit status, current branch
Merge feels confusingmental model of commit pointers is weakbranch list, history graph
Branches pile upno cleanup habitold feature branches after merge

Practical rule

If the change has a name, it can usually have its own branch.

Key takeaways

  • a branch is a safe line of work, not a scary advanced feature
  • branches help separate finished work from experiments
  • most Git collaboration becomes easier once branches make sense mentally

Official documentation