Git Merge
Merging is the primary way Git combines the work from two separate branches. Whether you're integrating a feature or catching up with the main code, understanding the technical "how" of a merge is essential for a clean history.
1. Fast-Forward Merge
A Fast-Forward merge occurs when the target branch (e.g., main) has NOT changed since you created your feature branch. Git simply "fast-forwards" the pointer to the latest commit.
Before vs. After
[!NOTE] No "Merge Commit" is created during a fast-forward merge.
2. Three-Way Merge
If both main and your feature branch have moved forward, a Three-Way Merge is required. Git finds a common ancestor and creates a new Merge Commit to tie the histories together.
Before vs. After
3. How to Merge
The standard workflow for integrating a feature:
# 1. Switch to the target branch
git switch main
# 2. Merge the feature
git merge feature-sidebar
# 3. Inspect the results
git log --oneline --graph --all4. Merge vs. Rebase: Comparison
| Feature | Merging | Rebasing |
|---|---|---|
| History | Preserve. Shows exactly how work evolved. | Clean. Shows a perfectly linear "story". |
| Commits | Creates a new "Merge Commit". | Rewrites existing commits. |
| Safety | High. Non-destructive. | Moderate. Destructive to history. |
| Best For | Public/Shared branches. | Personal/Local feature branches. |
[!TIP] Avoid the "Foxtrot" Merge To keep your history clean, try to ensure your feature branch is up-to-date with
mainbefore merging. You can do this by rebasing your local feature branch againstmainfirst, allowing for a clean Fast-Forward merge into the stable branch.