Git Branching - The Ultimate Guide for Beginners & Pros!
Master Git branching! Create, switch, delete, and work with branches! Git's most powerful feature!
Introduction
Branching is Git's superpower! Let's master it!
What You Will Learn
- What is a Git branch
- Creating, listing, switching branches
- Deleting branches
- Branch naming conventions
- Feature branch workflow
Prerequisites
What is a Branch?
A branch in Git is just a lightweight movable pointer to a commit! The default branch is usually main!
Think of branches as separate workspaces! You can experiment without affecting the main code!
graph LR
C1 -- main --> C2
C2 -- main --> C3
C2 -- feature-login --> C4
C4 -- feature-login --> C5
Git Branch Commands!
List Branches
# List local branches
git branch
# List all branches (local + remote)
git branch -a
Create a Branch
# Create a new branch (stay on current branch)
git branch feature-login
# Create AND switch to new branch (RECOMMENDED!)
git switch -c feature-login
# OR (old way)
git checkout -b feature-login
Switch Branches
# Switch to main branch
git switch main
# OR (old way)
git checkout main
Delete a Branch
# Delete a local branch that's been merged
git branch -d feature-login
# Force delete (DANGEROUS!)
git branch -D feature-login
The Feature Branch Workflow!
The most common Git workflow!
- Create a new branch from
mainfor each feature/bugfix! - Work on the branch!
- Commit your changes!
- Push to GitHub!
- Open a Pull Request!
- Merge!
- Delete the branch!
Branch Naming Conventions!
Use good, consistent names!
- Features:
feature/user-authentication,feature/checkout - Bugfixes:
bugfix/login-error,bugfix/header-layout - Hotfixes:
hotfix/critical-bug-prod - Releases:
release/v1.0,release/v1.1
Best Practices!
- Branch often: Small branches = easier merges!
- Keep branches focused: One branch = one feature/fix!
- Delete merged branches: Keep your repo clean!
- Pull main often: Avoid big merges later!
Summary
Git branching is a game-changer! Use branches to experiment safely!
Related Articles
Previous Tutorial
Next Tutorial
Let's learn merging! → Merging & Merge Conflicts