Git — Branches
What are Branches?
Branches let you develop features, fix bugs, or experiment in isolation from the main codebase. Each branch is an independent line of development.
# List branches
git branch
# Current branch has an asterisk
* main
feature-login
bugfix-header
Creating Branches
# Create a new branch
git branch feature-login
# Create and switch to it
git checkout -b feature-login
# Modern way (Git 2.23+)
git switch -c feature-login
Switching Branches
# Old way
git checkout feature-login
# Modern way
git switch feature-login
# Go back to previous branch
git switch -
Working with Branches
# Create a feature branch
git switch -c feature-dashboard
# Make changes
echo "dashboard" > dashboard.html
git add dashboard.html
git commit -m "feat: add dashboard page"
# Switch back to main
git switch main
# dashboard.html doesn't exist here yet!
# Merge the feature
git merge feature-dashboard
# Now dashboard.html is in main
Branch Naming
# Feature branches
feature/user-auth
feature/payment-integration
feature/dark-mode
# Bug fixes
bugfix/login-error
bugfix/mobile-responsive
# Hotfixes (urgent production fixes)
hotfix/security-patch
# Releases
release/v1.2.0
Viewing Branches
# Local branches
git branch
# All branches (local + remote)
git branch -a
# Branch with last commit
git branch -v
# Merged branches
git branch --merged
# Unmerged branches
git branch --no-merged
Deleting Branches
# Delete merged branch
git branch -d feature-login
# Force delete (unmerged)
git branch -D feature-login
# Delete remote branch
git push origin --delete feature-login
Comparing Branches
# See what's different
git diff main..feature-login
# See what feature has that main doesn't
git log main..feature-login
# See commits unique to each branch
git log --left-right main...feature-login
Common Branching Strategies
GitHub Flow (Simple)
main
└── feature-branch (create, commit, merge via PR)
Git Flow (Complex)
main
├── develop
│ ├── feature-1
│ ├── feature-2
│ └── release/v1.0
└── hotfix/critical-bug
Stashing Changes
Save uncommitted changes temporarily:
# Stash current changes
git stash
# List stashes
git stash list
# Apply most recent stash
git stash apply
# Apply and remove stash
git stash pop
# Drop a specific stash
git stash drop stash@{0}
Practical Example
# Working on feature, need to fix a bug
git stash
git switch main
git switch -c hotfix/typo
# fix the typo
git add . && git commit -m "fix: correct typo in header"
git switch main
git merge hotfix/typo
git branch -d hotfix/typo
# Back to feature
git switch feature-dashboard
git stash pop
Best Practices
- Always create branches for new features
- Name branches descriptively
- Delete merged branches to keep the repo clean
- Pull before creating a new branch
- Use pull requests for code review
- Never commit directly to
mainin team projects
Mini Practice
- Create a new branch and switch to it
- Make commits on the new branch
- Switch back to
mainand verify changes aren't there - Merge the branch into
main - Delete the merged branch
Up Next
Next: Merge →
Related Topics
Frequently Asked Questions about Branches
What is Branches in Git?
Branches is a fundamental concept in Git. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Branches?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Branches.
Why is Branches important in Git?
Branches is essential for Git development. Understanding this concept will help you write better code and solve real-world problems more effectively.