Learning Objectives
- Learn how to create and switch between branches
- Master essential branch management commands
- Understand how to rename and delete branches safely
- Practice common branch operations through hands-on examples
Creating Branches
Creating a new branch in Git is a lightweight operation that takes just a moment. There are several ways to create branches, depending on your needs:
Create a New Branch
# Create a new branch$ git branch feature-name # Create and switch to new branch$ git checkout -b feature-name # Modern way to create and switch (Git 2.23+)$ git switch -c feature-name
The -b
flag in checkout and -c
flag in switch both create a new branch.
Create from Specific Commit
# Create branch from specific commit$ git branch feature-name commit-hash # Create and switch to branch from commit$ git checkout -b feature-name commit-hash # Using switch command$ git switch -c feature-name commit-hash
Switching Between Branches
Git provides multiple ways to switch between branches. The modern switch
command is recommended, but checkout
is still widely used:
# Modern way to switch branches (recommended)$ git switch feature-name # Traditional way to switch branches$ git checkout feature-name # List all branches (* marks current branch)$ git branch # List all branches with more details$ git branch -v
Important Note
Before switching branches, always ensure your working directory is clean (no uncommitted changes) or stash your changes. Use git status
to check.
Branch Management
Effective branch management involves renaming, deleting, and keeping your branches organized:
Renaming Branches
# Rename current branch$ git branch -m new-name # Rename specific branch$ git branch -m old-name new-name
Deleting Branches
# Delete merged branch$ git branch -d branch-name # Force delete unmerged branch$ git branch -D branch-name
Branch Information
# Show merged branches$ git branch --merged # Show unmerged branches$ git branch --no-merged # Show branch details$ git branch -vv # Show all remote branches$ git branch -r
Common Scenarios
Here are some common scenarios you'll encounter when working with branches:
Starting New Feature
# Ensure you're on main branch$ git switch main # Update main branch$ git pull # Create and switch to feature branch$ git switch -c feature/new-feature # Work on your changes...$ git add .$ git commit -m "Add new feature"
Cleaning Up Branches
# List merged branches$ git branch --merged main # Delete merged branches$ git branch -d feature/completed # Force delete if needed$ git branch -D feature/abandoned # Clean up remote-tracking branches$ git remote prune origin
Best Practices for Branch Operations
- Keep branches updated:
Regularly sync your feature branches with the main branch to avoid complex merges later.
- Clean up regularly:
Delete branches after they're merged to keep your repository tidy.
- Use descriptive names:
Follow a consistent naming convention (e.g., feature/, bugfix/, hotfix/).
What's Next?
Now that you know how to work with branches, in the next lesson you'll learn about:
- Merging branches together
- Understanding different merge strategies
- Handling merge conflicts