Back to Learning Path

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

main branch → Your stable, production-ready code
↓ (branch off)
feature branch → Work on new features here
↓ (commit changes)
↓ (commit more changes)
↓ (merge back when ready)
main branch → Now includes your new feature!

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 (*).

bash
git branch

2. Create a New Branch

Create a new branch from your current position. Use descriptive names like feature-login or fix-bug-header.

bash
git branch feature-name

3. Switch to a Branch

Move to a different branch to work on it.

bash
git checkout feature-name

Pro 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).

bash
git branch -d feature-name

Hands-On Practice

Step 1: Check your current branch

bash
git branch

You should see * main or * master

Step 2: Create and switch to a new branch

bash
git checkout -b feature-test

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

bash
echo "Testing branches" > test.txt
git add test.txt
git commit -m "Add test file"

Step 4: Switch back to main

bash
git checkout main

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

bash
git merge feature-test

Now test.txt appears in main! Your changes are merged.

Step 6: Clean up (optional)

bash
git branch -d feature-test

Delete 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-profile instead of test123.

Branch Naming Best Practices

feature/user-authentication — For new features
fix/header-bug — For bug fixes
hotfix/security-patch — For urgent fixes
experiment/new-design — For experiments

Quick Summary

  • Branches let you work on features without affecting main code
  • Use git checkout -b name to create and switch to a new branch
  • Use git merge to combine branches
  • Always use descriptive branch names
  • Keep your main branch stable and production-ready