Branches Basics
Introduction
Branches let you work on features or fixes in isolation while main stays stable. In Git, a branch is a movable pointer to a commit—not a full copy of the project. This chapter creates branches, switches between them, and sees how they appear in git log.
Prerequisites
Why Branch?
main: A --- B --- C
\
feature: D --- ECode explanation:
maincontinues atCwhilefeatureadds commitsD,E- Merge (next chapter) combines lines of work
Without branches, experimental commits would land directly on production history.
View Branches
git branchRemote-tracking branches (after clone):
git branch -aCurrent branch marked with *.
Create and Switch
Modern commands:
git switch -c feature/loginCode explanation:
-ccreates branch and switches to it- Equivalent older form:
git checkout -b feature/login
Switch to existing branch:
git switch mainReturn to feature:
git switch feature/loginWork on a Feature Branch
git switch main
git switch -c feature/add-greetingEdit files:
echo "console.log('hi');" > greet.js
git add greet.js
git commit -m "Add greet script"main does not include greet.js until you merge.
Verify:
git switch main
ls greet.jsFile missing on main—expected.
git switch feature/add-greeting
ls greet.jsPresent on feature branch.
See History Across Branches
git log --oneline --graph --decorate --allExample:
* b2c3d4e (feature/add-greeting) Add greet script
* a1b2c3d (HEAD -> main) Initial commitWhen on main, feature commit still exists but is not ancestor of HEAD until merge.
Rename and Delete Branches
Rename current branch:
git branch -m new-nameDelete merged branch (safe):
git switch main
git branch -d feature/add-greetingForce delete unmerged:
git branch -D feature/abandonedWarning
Do Not Delete Shared Remote Branches Casually
Local delete is fine; deleting origin/feature affects teammates—coordinate first.
Branch Naming Conventions
| Pattern | Example |
|---|---|
| Feature | feature/user-login |
| Bugfix | fix/null-email |
| Release | release/1.2.0 |
| Personal (avoid on shared repo) | john-wip |
Use lowercase, hyphens, short descriptive names.
HEAD Detached (Awareness)
Checking out a raw commit hash puts you in detached HEAD:
git switch a1b2c3dYou can look around but new commits are orphaned unless you create a branch. Normal work stays on named branches.
Practice Exercise
- On
main, commit a README update - Create
feature/docsbranch - Add
docs/notes.md, commit - Switch to
main—confirmdocs/notes.mdabsent - Run
git log --oneline --graph --all
FAQ
How many branches?
As many as you need—delete after merge to reduce clutter.
Branch vs folder?
Branches are Git metadata; folders are files. Same folder tree, different commits checked out when you switch.
switch vs checkout?
git switch / git restore split old checkout duties—clearer for beginners.
Create branch without switching?
git branch feature/x then git switch feature/x later.
Remote branches?
origin/main appears after clone/fetch—chapter 11.
main vs master?
Same role; modern default is main.