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?

text
main:     A --- B --- C
                       \
feature:                D --- E

Code explanation:

  • main continues at C while feature adds commits D, E
  • Merge (next chapter) combines lines of work

Without branches, experimental commits would land directly on production history.

View Branches

bash
git branch

Remote-tracking branches (after clone):

bash
git branch -a

Current branch marked with *.

Create and Switch

Modern commands:

bash
git switch -c feature/login

Code explanation:

  • -c creates branch and switches to it
  • Equivalent older form: git checkout -b feature/login

Switch to existing branch:

bash
git switch main

Return to feature:

bash
git switch feature/login

Work on a Feature Branch

bash
git switch main
git switch -c feature/add-greeting

Edit files:

bash
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:

bash
git switch main
ls greet.js

File missing on main—expected.

bash
git switch feature/add-greeting
ls greet.js

Present on feature branch.

See History Across Branches

bash
git log --oneline --graph --decorate --all

Example:

text
* b2c3d4e (feature/add-greeting) Add greet script
* a1b2c3d (HEAD -> main) Initial commit

When on main, feature commit still exists but is not ancestor of HEAD until merge.

Rename and Delete Branches

Rename current branch:

bash
git branch -m new-name

Delete merged branch (safe):

bash
git switch main
git branch -d feature/add-greeting

Force delete unmerged:

bash
git branch -D feature/abandoned

Warning

Do Not Delete Shared Remote Branches Casually

Local delete is fine; deleting origin/feature affects teammates—coordinate first.

Branch Naming Conventions

PatternExample
Featurefeature/user-login
Bugfixfix/null-email
Releaserelease/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:

bash
git switch a1b2c3d

You can look around but new commits are orphaned unless you create a branch. Normal work stays on named branches.

Practice Exercise

  1. On main, commit a README update
  2. Create feature/docs branch
  3. Add docs/notes.md, commit
  4. Switch to main—confirm docs/notes.md absent
  5. 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.