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
bash1$ git checkout -b feature/login-form2Switched to a new branch 'feature/login-form'
This creates a new branch and switches to it in one command.
Modern Alternative
bash1$ git switch -c feature/login-form2Switched to a new branch 'feature/login-form'
The switch command is clearer but checkout -b is more widely known.
Create Without Switching
bash1$ git branch feature/login-form
This creates the branch but keeps you on the current branch.
Branch Naming Conventions
Use prefixes to categorize branches:
| Prefix | Purpose | Example |
|---|---|---|
feature/ | New functionality | feature/user-auth |
fix/ | Bug fixes | fix/login-crash |
hotfix/ | Urgent production fixes | hotfix/security-patch |
release/ | Release preparation | release/v2.0 |
chore/ | Maintenance tasks | chore/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
bash1$ git branch2 main3* feature/login-form
The * indicates your current branch.
List All Branches (Including Remote)
bash1$ git branch -a2 main3* feature/login-form4 remotes/origin/main5 remotes/origin/feature/api
Show Branch with Last Commit
bash1$ git branch -v2 main 1a2b3c4 Initial commit3* feature/login-form 5d6e7f8 Add login form
Switching Branches
Using checkout
bash1$ git checkout main2Switched to branch 'main'
Using switch (Modern)
bash1$ git switch main2Switched to branch 'main'
Caution: Uncommitted Changes
If you have uncommitted changes, Git may prevent switching:
bash1$ git switch main2error: Your local changes to the following files would be overwritten by checkout:3 login.js4Please commit your changes or stash them before you switch branches.
Options:
- Commit your changes
- Stash them:
git stash - Discard them:
git checkout -- .(dangerous!)
Deleting Branches
Delete Merged Branch
bash1$ git branch -d feature/login-form2Deleted branch feature/login-form (was 5d6e7f8).
The -d flag only works if the branch has been merged.
Force Delete Unmerged Branch
bash1$ git branch -D feature/experimental2Deleted branch feature/experimental (was 9a8b7c6).
Use -D (uppercase) to delete regardless of merge status. Be careful—unmerged work will be lost.
Delete Remote Branch
bash1$ git push origin --delete feature/login-form2To github.com:user/repo.git3 - [deleted] feature/login-form
Branch Workflow
A typical feature workflow:
bash1# 1. Start from updated main2$ git switch main3$ git pull45# 2. Create feature branch6$ git switch -c feature/new-widget78# 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"1314# 4. Push to remote15$ git push -u origin feature/new-widget1617# 5. Open pull request (covered next lesson)1819# 6. After merge, clean up20$ git switch main21$ git pull22$ git branch -d feature/new-widget
Visualizing Branches
See how branches diverge and merge:
bash1$ git log --oneline --graph --all2* 8a9b0c1 (HEAD -> feature/login-form) Add validation3* 7e8f9a0 Add login form4| * 6d7e8f9 (origin/main, main) Update README5|/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.