Compare Git Merge and Git Rebase in terms of history structure, and when is it too risky to use Rebase?
Git Merge creates a new merge commit connecting two branches, preserving historical context and the exact order of events as they occurred. Git Rebase copies your branch commits and reapplies them one by one on top of the target branch, producing a clean, linear history free of redundant merge commits. Golden Rule: Never rebase on shared public branches (such as main) that have been uploaded to the server, because Rebase rewrites hashes and breaks Your colleagues' drafts.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
Visual comparison:
-
After
git merge main:* commit 9a8b (Merge branch 'main' into feature) |\ | * commit 3e4f (main) * | commit 1a2b (feature) |/ * commit c0d1- Keeps the real history of fork and merge time.
- Generates an additional merge commit.
-
After
git rebase main:* commit 5g6h' (feature - completely new hash) * commit 3e4f (main) * commit c0d1- Organized, easy-to-read linear history with
git log. - Every commit in
featuregot a brand new SHA-1.
- Organized, easy-to-read linear history with
The Golden Rule of Rebase:
“Never rebase commits that exist outside your local repository and that other developers have based work on.”
- Applying git rebase to main branch or shared public release branches, forcing the entire team to resolve complex conflicts and a distorted history.
- Loss of context of when commits were actually written during Rebase, making knowing the actual merge date more difficult compared to Merge Commit.
- What is the Squash and Merge strategy and how do you balance the cleanliness of the main history with the details of the local branches?
- What do you do if you encounter recurring conflicts on each commit during an interactive Rebase?
What's the difference between Fast-Forward Merge and Three-Way Merge, and when does Git force a Merge Commit?
Fast-Forward occurs when there have been no new commits on the target branch (such as main) since the moment your branch forked; in this case, Git just moves the main pointer to the last commit in your branch without creating a new commit. A Three-Way Merge occurs when the target branch has received new concurrent commits; Git compares the common ancestor and the tips of both branches to create a merge commit that joins the two paths.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
Illustration with drawing:
-
Fast-Forward Merge:
Before merging: main: A ─── B \ feature: C ─── D After git merge feature: main: A ─── B ─── C ─── D (feature)maincursor moves forward only. -
Three-Way Merge (Divergence):
Before merging: main: A ─── B ─── E (new commits to main) \ feature: C ─── D After git merge feature: main: A ─── B ─── E ─────── M (New Merge Commit) \ / feature: C ─── D ────┘Git uses the common ancestor commit
BwithEandDto automatically calculate the merge.
- The belief is that a merge always creates a new commit; in the case of Fast-Forward no commit is created, which would hide the history of a separate feature branch unless you use the --no-ff flag.
- The merge failed due to conflicts on the same lines between commits of the two branches, requiring manual resolution of the conflict.
- Why do some organizations force the use of the --no-ff flag when merging feature branches?
- What is the difference between the old merge algorithm Recursive and the modern default algorithm ORT in Git?