CodeBlanks Logo Icon CodeBlanks

Rebase vs merge

Learn the historical difference between rebase and merge.

Rebase and merge philosophies

Merge and rebase achieve the same goal with different philosophies. merge preserves the exact points where branches split and converged; keeping a realistic record without altering history. In contrast, rebase eliminates branch bubbles and reorders commits to produce a clean and linear flow.

# Merge: branched graph (has merge commit)
git merge ozellik
# Rebase: straight linear graph (no merge commit)
git rebase main

Which method to choose depends on the team's history culture and workflow.

Choosing the right strategy

Rebasing shared branches invalidates commit references for your teammates; therefore, rebase should only be used on private local feature branches. When integrating into the main branch, teams either create a record using merge --no-ff or merge the flattened branch.

# Rebase to keep local branch up to date:
git rebase main
# Merge into shared main branch:
git merge --no-ff ozellik

This keeps local development tidy while safely guarding team history.

Practice See total commit count with merge commit

Merge the ozellik branch with git merge --no-ff -m "Merge islemi" ozellik and print the total commit count with git rev-list --count HEAD.

Starter code

terminal
git init -q
git config user.name "Selin"
git config user.email "selin@example.com"
echo "1" > dosya.txt
git add dosya.txt
git commit -qm "Ilk surum"

git checkout -q -b ozellik
echo "2" > ozellik.txt
git add ozellik.txt
git commit -qm "Yeni ozellik"

git checkout -q main
echo "3" > ana.txt
git add ana.txt
git commit -qm "Ana dal ilerledi"

# ozellik dalini --no-ff ile merge et ve commit sayisini yazdir

Sample solution

terminal
git init -q
git config user.name "Selin"
git config user.email "selin@example.com"
echo "1" > dosya.txt
git add dosya.txt
git commit -qm "Ilk surum"

git checkout -q -b ozellik
echo "2" > ozellik.txt
git add ozellik.txt
git commit -qm "Yeni ozellik"

git checkout -q main
echo "3" > ana.txt
git add ana.txt
git commit -qm "Ana dal ilerledi"

git merge --no-ff -m "Merge islemi" ozellik
git rev-list --count HEAD

Expected output

4

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the Golden Rule of Rebase?

  • Never rebase branches that are shared publicly Correct answer
  • Rebase is prohibited only in repositories with a single developer
  • All commits in the repo must be deleted before rebasing
  • Merge commits always execute faster than rebase

Explanation: Because rebase rewrites commit hashes, rebasing a shared public branch disrupts the history for team members building on it.

2 Inspect history as a graph

Question
git log ____ --oneline
Answer
git log --graph --oneline

Explanation: The --graph option draws an ASCII graph in the terminal showing branch divergency and merges.

3 Merge vs rebase comparison

Term Meaning
git merge Preserves true chronological history
git rebase Produces linear and flat history
Altın Kural Do not rebase shared public branch
--graph Draws branch structure in terminal

All lessons in Intermediate

20 Intermediate final

  1. Branch management
  2. Rebase
  3. Conflict
  4. PR flow