Combining Parallel Universes
You've developed a feature in a branch. Now it's time to integrate it back into the main timeline with git merge.
Basic Merge
To merge a feature branch into main:
# Switch to the branch you want to merge INTO
git switch main
# Merge the feature branch
git merge feature-branch
Types of Merges
Fast-Forward Merge
When no commits were made to main while you worked on your feature, Git simply moves the pointer forward:
Updating a1b2c3d..e4f5g6h
Fast-forward
navigation.js | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
No merge commit is created—just a clean, linear history.
Three-Way Merge
When both branches have new commits, Git creates a merge commit:
Merge made by the 'recursive' strategy.
sensors.js | 15 +++++++++++++++
1 file changed, 15 insertions(+)
This merge commit has two parents—one from each branch.
Merge Conflicts
When both branches modified the same lines:
Auto-merging navigation.js
CONFLICT (content): Merge conflict in navigation.js
Automatic merge failed; fix conflicts and then commit the result.
Resolving Conflicts
Open the conflicted file:
function calculateSpeed() {
<<<<<<< HEAD
return distance / 1500; // main branch version
=======
return distance / 2000; // feature branch version
>>>>>>> feature-propulsion
}
Edit to resolve:
function calculateSpeed() {
return distance / 2000; // Use feature branch speed
}
Then complete the merge:
git add navigation.js
git commit
Aborting a Merge
If a merge goes wrong:
git merge --abort
Returns to pre-merge state.
Merge Strategies
No Fast-Forward
Always create a merge commit:
git merge --no-ff feature-branch
Preserves branch history in the graph.
Squash Merge
Combine all feature commits into one:
git merge --squash feature-branch
git commit -m "Add complete sensor system"
Creates cleaner history but loses individual commit details.
Viewing Merge Status
See what's merged:
git branch --merged # Branches merged into current
git branch --no-merged # Branches not yet merged
Conflict Resolution Tools
Use a visual merge tool:
git mergetool
Opens configured merge tool (VS Code, Meld, KDiff3, etc.).
Best Practices
- Merge frequently to avoid large conflicts
- Pull main into your feature branch regularly
- Test after merging before pushing
- Use meaningful merge commit messages
- Consider squash merging for messy feature branches
Merge Workflow Example
# Finish feature work
git switch feature-sensors
git add .
git commit -m "Complete sensor calibration"
# Update feature branch with latest main
git switch main
git pull
git switch feature-sensors
git merge main # Resolve any conflicts
# Merge feature into main
git switch main
git merge feature-sensors
# Push updated main
git push
# Clean up
git branch -d feature-sensors
git push origin --delete feature-sensors
Merge vs Rebase
Two ways to integrate changes:
- Merge: Preserves complete history, creates merge commits
- Rebase: Creates linear history, rewrites commits (covered in Phase 29)
Both have their place. Merge is safer for beginners.
Next: Mission Corrections
You can now merge timelines. Next, learn to undo mistakes—because everyone makes them!