Understanding Branches
Branches are one of Git’s most powerful features, allowing developers to work on different features or fixes simultaneously without interfering with each other.
Branch Basics
# List all branches (* indicates current branch)
git branch
# Create a new branch
git branch feature-name
# Create and switch to a new branch
git checkout -b feature-name
# Switch to an existing branch
git checkout feature-name
# Delete a branch (after merging)
git branch -d feature-name
# Force delete a branch (even if not merged)
git branch -D feature-name
Branch Visualization
Branches in Git can be visualized as separate lines of development that can later be integrated:
A---B---C feature-branch
/
D---E---F---G main
Integrating Changes: Merge vs. Rebase
There are two primary ways to integrate changes from one branch into another: merging and rebasing. Each has its advantages and appropriate use cases.
Merging
Merging creates a new “merge commit” that combines the changes from both branches.
# Switch to the target branch (e.g., main)
git checkout main
# Merge changes from feature branch
git merge feature-branch
Result of merging:
A---B---C feature-branch
/ \
D---E---F---G---H main
Advantages of Merging:
- Preserves complete history and chronological order
- Non-destructive operation (doesn’t change existing commits)
- Clearly shows when and how features were integrated
Best for:
- Public/shared branches
- When the branch history is important to preserve
- When you want to see exactly when a feature was merged
Rebasing
Rebasing moves or “replays” your branch commits onto the tip of another branch, creating a linear history.
# Switch to the feature branch
git checkout feature-branch
# Rebase onto main
git rebase main
Before rebase:
A---B---C feature-branch
/
D---E---F---G main
After rebase:
A'--B'--C' feature-branch
/
D---E---F---G main
Advantages of Rebasing:
- Creates a cleaner, linear project history
- Eliminates unnecessary merge commits
- Makes it easier to find bugs with tools like
git bisect
Best for:
- Local/private branches before sharing
- Keeping feature branches up-to-date with main
- When you want a clean, linear history
Choosing Between Merge and Rebase
A good rule of thumb:
- Rebase your private/local branches to keep them updated with the main branch
- Merge your feature branches back into the main branch when they’re complete
The Golden Rule of Rebasing
Never rebase branches that others are working on or that have been pushed to a public repository.
Rebasing changes commit history, which can cause serious problems for collaborators if they’ve based work on those commits.
Advanced Branch Operations
Cherry-picking
Cherry-picking allows you to apply specific commits from one branch to another.
# Apply a specific commit to current branch
git cherry-pick commit-hash
Interactive Rebasing
Interactive rebasing gives you fine-grained control over your commit history.
# Start an interactive rebase for the last 3 commits
git rebase -i HEAD~3
This opens an editor where you can:
- Reorder commits
- Edit commit messages
- Combine (squash) commits
- Split commits
- Delete commits
Stashing Changes
Stashing allows you to temporarily save changes without committing them.
# Stash current changes
git stash
# List stashes
git stash list
# Apply most recent stash
git stash apply
# Apply specific stash
git stash apply stash@{2}
# Remove most recent stash after applying
git stash pop
# Clear all stashes
git stash clear