CodeBlanks Logo Icon CodeBlanks

What is rebase?

Learn how git rebase rewrites commits.

What is rebase?

The git rebase command moves the base of a branch to a new starting commit. It temporarily sets aside your branch commits, applies the target branch's new commits, and then replays your commits one by one on top. The result is a completely flat and linear history without merge bubbles.

git checkout ozellik-dali
git rebase main

This makes project history look as if all changes were made sequentially in a single straight line.

History rewriting effect

Although the content of commits is preserved during a rebase, their parents and timestamps change, so their commit hashes are completely rewritten. Rebase is therefore a history rewriting tool. A linear history makes tools like git log and debugging utilities much easier to use.

# rebase feature branch onto main
git rebase main
# if conflicts occur, resolve and continue
git rebase --continue

After rebase, the branch looks as clean as if it was just branched off the latest main.

Practice Make a branch linear with rebase

Rebase the gelistirme branch with git rebase -q main and list all commit subjects using git log --format="%s".

Starter code

terminal
git init -q
git config user.name "Mert"
git config user.email "mert@example.com"
echo "taban" > temel.txt
git add temel.txt
git commit -qm "Temel commit"

git checkout -q -b gelistirme
echo "gelistirme 1" > modul.txt
git add modul.txt
git commit -qm "Modul eklendi"

git checkout -q main
echo "ana guncelleme" > guncel.txt
git add guncel.txt
git commit -qm "Ana guncellendi"

git checkout -q gelistirme

# gelistirme dalini main uzerine rebase et ve log basliklarini listele

Sample solution

terminal
git init -q
git config user.name "Mert"
git config user.email "mert@example.com"
echo "taban" > temel.txt
git add temel.txt
git commit -qm "Temel commit"

git checkout -q -b gelistirme
echo "gelistirme 1" > modul.txt
git add modul.txt
git commit -qm "Modul eklendi"

git checkout -q main
echo "ana guncelleme" > guncel.txt
git add guncel.txt
git commit -qm "Ana guncellendi"

git checkout -q gelistirme

git rebase -q main
git log --format="%s"

Expected output

Modul eklendi
Ana guncellendi
Temel commit

Questions and answers

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

1 What happens to commits when git rebase main is run on a feature branch?

  • Only unstaged changes in the working directory are moved to stage
  • Commits are replayed onto the tip of main and get new hash values Correct answer
  • Commits are deleted and the branch turns directly into main
  • A new merge commit with two parents is created to join both branches

Explanation: Because rebase copies and replays commits onto the new base, it rewrites history and produces new commit hashes.

2 Start rebase operation

Question
git ____ main
Answer
git rebase main

Explanation: git rebase main moves the current branch onto the tip commit of main.

3 Rebase concepts

Term Meaning
git rebase <hedef> Moves branch onto target commit
Yeniden yazma Regenerates commit hashes
Doğrusal tarihçe Straight flow without branch bubbles
--continue Continues rebase after resolution

All lessons in Intermediate

20 Intermediate final

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