Git Branch
Create, switch, and manage parallel lines of development
Overview
Branches let you work on features or fixes in isolation without affecting main. Git branches are lightweight pointers to commits. Create a branch, make commits, then merge or open a pull request.
Syntax / Usage
# List branches
git branch
git branch -a # include remotes
# Create and switch
git switch -c feature/login
# or: git checkout -b feature/login
# Switch existing branch
git switch main
# Delete branch
git branch -d feature/login # safe delete
git branch -D feature/login # force delete
# Rename current branch
git branch -m new-name
Examples
Typical feature workflow:
git switch main
git pull origin main
git switch -c feature/dark-mode
# ... make changes ...
git add .
git commit -m "feat(ui): add dark mode toggle"
git push -u origin feature/dark-mode
Compare branch to main:
git log main..feature/dark-mode --oneline
git diff main...feature/dark-mode
Common Mistakes
- Working directly on
mainfor feature development - Long-lived branches that diverge heavily from main
- Forgetting
-uon first push, then wondering why upstream is missing - Deleting a branch before merging its work
See Also
commit merge rebase remote