Working with Git Branches
In this lesson, we'll explore one of Git's most powerful features: branches. Think of branches as parallel universes where you can develop features, fix bugs, or experiment without affecting the main codebase until you're ready.
Learning Goals:
- Understand what Git branches are and why they're useful
- Create, list, and switch between branches
- Learn how to merge branches back together
- Develop confidence working with multiple branches
What Are Git Branches?
A Git branch is essentially a lightweight movable pointer to a commit. When you create a new branch, you're creating a new pointer that you can move around independently of other branches.
The default branch is typically called main (or master in older repositories). Every time you commit, the current branch pointer moves forward automatically.
main: A---B---C
feature: \
D---E
Creating and Listing Branches
Let's start by exploring the branches in your repository.
git branch
To create a new branch:
git branch feature/login
This creates the branch but doesn't switch to it. You can verify it exists:
git branch
Use descriptive branch names that indicate the purpose, like feature/user-authentication, bugfix/login-error, or hotfix/critical-bug.
Switching Between Branches
To start working on your new branch, you need to switch to it:
git checkout feature/login
Or use the shorter combined command:
git checkout -b feature/login
Let's make some changes on our new branch:
echo "// New login feature" >> login.js
git add login.js
git commit -m "Add login feature skeleton"
Understanding HEAD and Branch Pointers
The HEAD is a special pointer that refers to the current branch you're working on. When you switch branches, HEAD moves to point to the new branch.
git log --oneline -1
git status
Comparing Branches
You can see what's different between branches:
git diff main
Or compare any two branches:
git diff main..feature/login
Merging Branches (Preview)
While we'll cover merging in detail in the next lesson, here's a quick preview of how to bring your feature work back to the main branch:
git checkout main
git merge feature/login
Always ensure your working directory is clean (no uncommitted changes) before switching branches. Use git status to check, and commit or stash any changes first.
Common Branch Operations
Here are the essential branch commands you'll use daily:
- Create Branches
- List & Navigate
- Clean Up
# Create branch
git branch new-feature
# Create and switch
git checkout -b new-feature
# List branches
git branch
# List remote branches
git branch -r
# List all branches
git branch -a
# Switch branch
git checkout branch-name
# Delete merged branch
git branch -d old-branch
# Force delete unmerged branch
git branch -D unwanted-branch
Common Pitfalls
- Forgotten branch switches: Always check which branch you're on with
git statusorgit branch - Working on the wrong branch: Commit or stash changes before switching branches
- Long-lived feature branches: Try to keep branches short-lived to avoid complex merges
- Unclear branch names: Use descriptive names that your team understands
- Not deleting old branches: Clean up merged branches to reduce clutter
Summary
Git branches are powerful tools that enable parallel development. You can:
- Create isolated environments for features, fixes, or experiments
- Switch between contexts quickly and safely
- Compare work across different branches
- Keep your main branch stable while developing new features
Remember: branches are cheap and easy to create, so use them liberally!
Quiz
Working with Branches – Quick Check
What command creates a new branch but does not switch to it?