Branches
Create, switch, and manage parallel lines of work
Skills taught: git.branches, git.checkout, git.switch
Creating and switching branches
# List all branches (* = current)
git branch
# * main
# feature/dark-mode
# Create a new branch
git branch feature/user-profile
# Switch to it (modern command)
git switch feature/user-profile
# Create and switch in one step
git switch -c feature/user-profile
# (older equivalent: git checkout -b feature/user-profile)
# See all branches including remotes
git branch -a
# Delete a branch (after merging)
git branch -d feature/user-profile
# Force delete (unmerged changes — be careful)
git branch -D feature/user-profile
Typical feature branch workflow
# 1. Start from an up-to-date main
git switch main
git pull
# 2. Create feature branch
git switch -c feature/dark-mode
# 3. Work and commit
echo '.dark { background: #000 }' >> style.css
git add style.css
git commit -m "Add dark mode base styles"
# 4. More commits...
git commit -m "Add dark mode toggle button"
git commit -m "Persist dark mode preference in localStorage"
# 5. Merge back to main (or open a Pull Request)
git switch main
git merge feature/dark-mode
# 6. Push
git push
Frequently Asked Questions
How should I name my branches?
Use lowercase with hyphens. Prefix by type: feature/user-auth, fix/login-redirect, chore/upgrade-deps, docs/readme-update. Many teams use issue numbers: feature/123-user-auth. Avoid spaces, dots, or special characters.
What happens to my uncommitted changes when I switch branches?
If the changes don't conflict with the target branch, Git carries them over (they stay in your working tree). If they conflict, Git refuses to switch — you must commit or stash them first: git stash.