Step 7: Working with Branches
Branches let you work on new features without breaking your main code. Think of them as parallel universes for your project — you can experiment safely, then merge changes back when ready.
Why Use Branches?
- •Work on features independently — Build new features without affecting the main codebase.
- •Experiment safely — Try new ideas without fear of breaking working code.
- •Collaborate better — Multiple developers can work on different features simultaneously.
- •Keep main code stable — Your main branch stays clean and deployable at all times.
Understanding Branches Visually
Analogy: Imagine writing a book. The main branch is your published version. When you want to add a new chapter, you create a draft (branch), work on it separately, and only add it to the published book when it's ready.
Essential Branch Commands
1. View All Branches
See all branches in your repository. The current branch is marked with an asterisk (*).
git branch2. Create a New Branch
Create a new branch from your current position. Use descriptive names like feature-login or fix-bug-header.
git branch feature-name3. Switch to a Branch
Move to a different branch to work on it.
git checkout feature-namePro tip: Use git checkout -b feature-name to create and switch in one command!
4. Delete a Branch
Remove a branch after you've merged it (Git won't let you delete unmerged branches unless you force it with -D).
git branch -d feature-nameHands-On Practice
Step 1: Check your current branch
git branchYou should see * main or * master
Step 2: Create and switch to a new branch
git checkout -b feature-testThis creates a branch called feature-test and switches to it immediately.
Step 3: Make some changes
Create or edit a file, then commit your changes:
echo "Testing branches" > test.txt
git add test.txt
git commit -m "Add test file"Step 4: Switch back to main
git checkout mainNotice that your test.txt file disappears! Don't worry — it's safe in the feature-test branch.
Step 5: Merge your branch
Bring changes from feature-test into main:
git merge feature-testNow test.txt appears in main! Your changes are merged.
Step 6: Clean up (optional)
git branch -d feature-testDelete the branch since we're done with it.
Try It Yourself
Practice creating branches with meaningful names for different features. Try creating feature-login, fix-typo, or experiment-design. The more you practice, the more natural it becomes!
Common Mistakes
- •Forgetting to commit before switching branches — Uncommitted changes can follow you to the new branch or cause conflicts.
- •Working directly on main — Always create a feature branch instead. Keep main stable!
- •Using confusing branch names — Use clear names like
feature-user-profileinstead oftest123.
Branch Naming Best Practices
Quick Summary
- Branches let you work on features without affecting main code
- Use
git checkout -b nameto create and switch to a new branch - Use
git mergeto combine branches - Always use descriptive branch names
- Keep your main branch stable and production-ready