Git
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.
Exercise 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".
Show example solution
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"
Test yourself
-
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
- Commits are deleted and the branch turns directly into main
- A new merge commit with two parents is created to join both branches
Show answer
Commits are replayed onto the tip of main and get new hash values — Because rebase copies and replays commits onto the new base, it rewrites history and produces new commit hashes.
-
Start rebase operation
git ____ main
Show answer
git rebase main
-
Rebase concepts
- 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
Next lesson: Rebase vs merge