12 minlesson

Working with Branches

Working with Branches

Branches let you work on features, fixes, or experiments in isolation without affecting the main codebase. This is fundamental to collaborative development.

Why Branch?

Without branches:

  • Every commit goes directly to main
  • Work-in-progress breaks the codebase
  • Multiple developers constantly conflict
  • No way to review before merging

With branches:

  • Features develop in isolation
  • Main stays stable and deployable
  • Changes are reviewed via pull requests
  • Easy to discard failed experiments

Creating Branches

Create and Switch

bash
1$ git checkout -b feature/login-form
2Switched to a new branch 'feature/login-form'

This creates a new branch and switches to it in one command.

Modern Alternative

bash
1$ git switch -c feature/login-form
2Switched to a new branch 'feature/login-form'

The switch command is clearer but checkout -b is more widely known.

Create Without Switching

bash
1$ git branch feature/login-form

This creates the branch but keeps you on the current branch.

Branch Naming Conventions

Use prefixes to categorize branches:

PrefixPurposeExample
feature/New functionalityfeature/user-auth
fix/Bug fixesfix/login-crash
hotfix/Urgent production fixeshotfix/security-patch
release/Release preparationrelease/v2.0
chore/Maintenance taskschore/update-deps

Good branch names are:

  • Lowercase with hyphens
  • Descriptive but concise
  • Include ticket numbers if applicable: feature/JIRA-123-user-auth

Viewing Branches

List Local Branches

bash
1$ git branch
2 main
3* feature/login-form

The * indicates your current branch.

List All Branches (Including Remote)

bash
1$ git branch -a
2 main
3* feature/login-form
4 remotes/origin/main
5 remotes/origin/feature/api

Show Branch with Last Commit

bash
1$ git branch -v
2 main 1a2b3c4 Initial commit
3* feature/login-form 5d6e7f8 Add login form

Switching Branches

Using checkout

bash
1$ git checkout main
2Switched to branch 'main'

Using switch (Modern)

bash
1$ git switch main
2Switched to branch 'main'

Caution: Uncommitted Changes

If you have uncommitted changes, Git may prevent switching:

bash
1$ git switch main
2error: Your local changes to the following files would be overwritten by checkout:
3 login.js
4Please commit your changes or stash them before you switch branches.

Options:

  1. Commit your changes
  2. Stash them: git stash
  3. Discard them: git checkout -- . (dangerous!)

Deleting Branches

Delete Merged Branch

bash
1$ git branch -d feature/login-form
2Deleted branch feature/login-form (was 5d6e7f8).

The -d flag only works if the branch has been merged.

Force Delete Unmerged Branch

bash
1$ git branch -D feature/experimental
2Deleted branch feature/experimental (was 9a8b7c6).

Use -D (uppercase) to delete regardless of merge status. Be careful—unmerged work will be lost.

Delete Remote Branch

bash
1$ git push origin --delete feature/login-form
2To github.com:user/repo.git
3 - [deleted] feature/login-form

Branch Workflow

A typical feature workflow:

bash
1# 1. Start from updated main
2$ git switch main
3$ git pull
4
5# 2. Create feature branch
6$ git switch -c feature/new-widget
7
8# 3. Work on feature (multiple commits)
9$ git add .
10$ git commit -m "feat: add widget component"
11$ git add .
12$ git commit -m "feat: style widget"
13
14# 4. Push to remote
15$ git push -u origin feature/new-widget
16
17# 5. Open pull request (covered next lesson)
18
19# 6. After merge, clean up
20$ git switch main
21$ git pull
22$ git branch -d feature/new-widget

Visualizing Branches

See how branches diverge and merge:

bash
1$ git log --oneline --graph --all
2* 8a9b0c1 (HEAD -> feature/login-form) Add validation
3* 7e8f9a0 Add login form
4| * 6d7e8f9 (origin/main, main) Update README
5|/
6* 5c6d7e8 Initial commit

Key Takeaway

Branches isolate work. Create a branch for every feature or fix. Keep main stable. Use meaningful branch names. Delete branches after merging to keep the repository tidy.