Git - Branching Basics
Intermediate
Branching is Git's killer feature. It allows you to diverge from the main line of development and work on features, experiments, or fixes in isolation, then merge them back when ready.
What are Branches?
A branch is simply a movable pointer to a specific commit. Think of it as a lightweight, movable label for a line of development:
main branch: A---B---C---D ← main points here
\
feature branch: E---F ← feature points here
Key concepts:
- HEAD: Points to the current branch
- Branch: A pointer to a commit
- Working Directory: Files as they exist for the current branch
- Branching: Creating a new pointer
- Switching: Moving HEAD to different branch
Viewing Branches
List Local Branches
git branch
Expected Output:
feature-login
* main ← * indicates current branch
bugfix-auth
List All Branches
# Include remote branches
git branch -a
git branch --all
Expected Output:
feature-login
* main
bugfix-auth
remotes/origin/HEAD -> origin/main
remotes/origin/main
remotes/origin/feature-login
Verbose Branch Information
# Show last commit on each branch
git branch -v
git branch --verbose
Expected Output:
feature-login a1b2c3d Add login form validation
* main b2c3d4e Update project documentation
bugfix-auth c3d4e5f Fix authentication timeout
Creating Branches
Create New Branch
# Create branch from current commit
git branch feature-user-profile
# Create branch from specific commit
git branch hotfix-v1.2.1 a1b2c3d
# Create branch from tag
git branch release-prep v1.2.0
Creating a branch doesn't switch to it automatically. It just creates the pointer.
Branch Naming Conventions
Good branch names:
feature/user-authentication
bugfix/login-timeout
hotfix/security-patch
release/v2.1.0
chore/update-dependencies
Branch naming patterns:
feature/JIRA-123-user-profile ← Include ticket number
bugfix/issue-456-memory-leak ← Reference issue
username/feature-description ← Personal branches
release/2023-10-25 ← Date-based releases
Switching Branches
Using git checkout (Classic)
# Switch to existing branch
git checkout feature-login
# Create and switch in one command
git checkout -b new-feature
# Create from specific commit and switch
git checkout -b hotfix a1b2c3d
Using git switch (Modern - Git 2.23+)
# Switch to existing branch
git switch feature-login
# Create and switch in one command
git switch -c new-feature
git switch --create new-feature
# Create from specific commit and switch
git switch -c hotfix a1b2c3d
git switch
is the modern, clearer alternative to git checkout
for branch operations.
What Happens When You Switch
When you switch branches, Git:
1. Updates HEAD to point to the new branch
2. Updates working directory files to match the branch
3. Updates the index/staging area
4. May refuse if you have uncommitted changes
Practical Branching Workflow
Feature Development Workflow
1. Start from main branch:
git switch main
git pull origin main # Get latest changes
2. Create feature branch:
git switch -c feature/user-dashboard
3. Work on the feature:
# Make changes
echo "Dashboard component" > dashboard.js
git add dashboard.js
git commit -m "Add basic dashboard component"
# More changes
echo "Dashboard styles" > dashboard.css
git add dashboard.css
git commit -m "Add dashboard styling"
4. Switch back to main when needed:
git switch main
# Work on main or create other branches
5. Return to feature work:
git switch feature/user-dashboard
# Continue development
Managing Multiple Branches
Tracking Branch Status
# Show branches with tracking info
git branch -vv
Expected Output:
feature-login a1b2c3d [origin/feature-login: behind 2] Add login form
* main b2c3d4e [origin/main] Update documentation
bugfix-auth c3d4e5f Fix authentication timeout
Branch Relationships
# Show which branches contain specific commit
git branch --contains a1b2c3d
# Show branches that haven't been merged
git branch --no-merged
# Show branches that have been merged
git branch --merged
Working with Uncommitted Changes
Stashing Before Switch
When you have uncommitted changes:
# This will fail if changes conflict
git switch other-branch
# Save changes temporarily
git stash
git switch other-branch
# Work on other branch
git switch original-branch
git stash pop # Restore changes
Carrying Changes to New Branch
# Create new branch with current changes
git switch -c new-feature
# Your uncommitted changes come with you
Deleting Branches
Safe Branch Deletion
# Delete merged branch (safe)
git branch -d feature-completed
# Force delete unmerged branch (dangerous)
git branch -D experimental-feature
-D
will delete branches even if they haven't been merged. Make sure you don't need the commits!
Deleting Remote Branches
# Delete remote branch
git push origin --delete feature-branch
# Alternative syntax
git push origin :feature-branch
Branch Comparison
Comparing Branches
# Show commits in feature not in main
git log main..feature-branch
# Show files that differ
git diff main..feature-branch --name-only
# Show detailed differences
git diff main..feature-branch
Finding Common Ancestor
# Find merge base (common ancestor)
git merge-base main feature-branch
# Show commits since divergence
git log $(git merge-base main feature-branch)..feature-branch
Remote Branch Operations
Tracking Remote Branches
# Create local branch tracking remote
git switch -c feature-login origin/feature-login
# Shorter version (Git will auto-track)
git switch feature-login # If remote branch exists
# Set upstream for existing branch
git push -u origin feature-login
Fetching Remote Branches
# Fetch all remote branches
git fetch origin
# See new remote branches
git branch -r
# Switch to new remote branch
git switch new-remote-feature
Advanced Branch Operations
Renaming Branches
# Rename current branch
git branch -m new-name
# Rename any branch
git branch -m old-name new-name
# Update remote after rename
git push origin -u new-name
git push origin --delete old-name
Branch Configuration
# Set default branch name
git config --global init.defaultBranch main
# Always setup tracking
git config --global branch.autosetupmerge always
# Show branch descriptions
git config branch.feature-login.description "User login functionality"
Branch Strategies
Git Flow
Git Flow branch types:
main/master ← Production-ready code
develop ← Integration branch
feature/* ← New features
release/* ← Release preparation
hotfix/* ← Production fixes
GitHub Flow
Simpler workflow:
main ← Always deployable
feature/* ← All work in feature branches
Deploy from feature branches
Merge to main when ready
Troubleshooting Branch Issues
Can't Switch Branches
Error:
error: Your local changes to the following files would be overwritten by checkout:
file.txt
Please commit your changes or stash them before you switch branches.
Solutions:
# Option 1: Commit changes
git add .
git commit -m "Work in progress"
# Option 2: Stash changes
git stash
git switch other-branch
# Option 3: Force switch (loses changes!)
git switch -f other-branch
Lost Branch Reference
# Find lost commits
git reflog
# Recreate branch from reflog
git branch recovered-branch HEAD@{5}
Branch Aliases and Shortcuts
# Useful branch aliases
git config --global alias.br branch
git config --global alias.sw switch
git config --global alias.co checkout
git config --global alias.cob "checkout -b"
git config --global alias.swc "switch -c"
Visual Branch Tools
# ASCII graph of branches
git log --graph --oneline --all
# More detailed graph
git log --graph --pretty=format:'%h -%d %s (%cr) <%an>' --abbrev-commit --all
# Show branch relationships
git show-branch
Summary
You now understand Git branching basics:
- ✅ Branches are lightweight pointers to commits
- ✅
git branch
creates branches, doesn't switch to them - ✅
git switch
is modern way to change branches - ✅ Feature branches isolate development work
- ✅ Multiple branches can exist simultaneously
- ✅ Branches can be merged, deleted, and renamed
- ✅ Remote tracking enables collaboration
Next Steps
Now that you understand branches, let's learn how to combine work from different branches:
Practice Tip: Create a practice repository and experiment with multiple branches. Try switching between them and see how your working directory changes.