Git/
Lesson

You have learned to merge branches, but merging creates a tangled history with merge commits that clutter git log. There is another way to integrate changes that keeps your history looking like a straight line. It is called rebaseWhat is rebase?A Git operation that replays your branch's commits on top of another branch's latest commit, creating a linear history instead of a merge commit., and understanding when to use it versus when to merge is a critical skill that AI tools consistently get wrong.

Think of rebase like editing a film. You shot some scenes (commits) on your feature branch while the main storyline moved forward. Rebase lets you re-edit your scenes so they appear to come right after the latest main storyline, as if you had started from the updated version all along.

AI assistants like ChatGPT routinely suggest git rebase on branches that teammates are actively working on. They treat rebase and merge as interchangeable, but rebasing a shared branch rewrites history that other people depend on. This is the most common and most dangerous rebase mistake.

How rebaseWhat is rebase?A Git operation that replays your branch's commits on top of another branch's latest commit, creating a linear history instead of a merge commit. works

When you run git rebase main on your feature branch, Git performs three steps behind the scenes:

  1. Saves your feature branch commits to a temporary area
  2. Moves your branch pointer to the tip of main
  3. Replays your commits one by one on top of the new base

Before rebase:

main:    A---B---C---D
              \
feature:       E---F---G

After rebase:

main:    A---B---C---D
                        \
feature:                 E'--F'--G'

Notice the prime symbols (E', F', G'). These are new commits with different hashes because Git recreated them on a different base. The code changes are identical, but Git sees them as entirely different commits.

02

Why rebaseWhat is rebase?A Git operation that replays your branch's commits on top of another branch's latest commit, creating a linear history instead of a merge commit. matters

BenefitExplanation
Linear historygit log reads like a chronological story, no merge commit clutter
Cleaner blamegit blame shows exactly when each line was introduced
Better bisectgit bisect finds bugs faster with a linear timeline
Professional PRsPull requests show a clean sequence of logical commits

Compare the two approaches side by side:

Merge history:

*   abc1234 Merge branch 'feature' into main
|\
| * def5678 Add search feature
| * ghi9012 Add database index
|/
* jkl3456 Update dependencies
* mno7890 Fix login bug

Rebase history:

* abc1234 Add search feature
* def5678 Add database index
* ghi9012 Update dependencies
* jkl3456 Fix login bug

The rebase history reads top to bottom like a clean changelog. No merge commits, no branching arrows.

03

The golden rule: never rebaseWhat is rebase?A Git operation that replays your branch's commits on top of another branch's latest commit, creating a linear history instead of a merge commit. shared commits

This is the single most important thing to understand about rebase. Rebase rewrites history. Those new commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. hashes mean that anyone who has your old commits will see a completely different timeline when they pull.

# NEVER DO THIS:
git checkout main
git rebase feature     # Rewrites main's history
git push --force       # Breaks everyone's repository

When to rebase vs merge:

Use rebase when...Use merge when...
Your branch is local and unpushedThe branch is shared with teammates
You want clean history before a PRYou want to preserve exact history
You are updating your branch with mainYou are integrating a finished feature into main
You are cleaning up commits before reviewYou are closing a long-running branch
04

Basic rebaseWhat is rebase?A Git operation that replays your branch's commits on top of another branch's latest commit, creating a linear history instead of a merge commit. workflow

Here is the typical flow for keeping a feature branch up to date:

# 1. Make sure you are on your feature branch
git checkout feature/add-search

# 2. Fetch the latest changes from remote
git fetch origin

# 3. Rebase your commits on top of latest main
git rebase origin/main

# 4. If there are conflicts, resolve them
# (edit files, then:)
git add .
git rebase --continue

# 5. Push your rebased branch (force needed because history changed)
git push --force-with-lease
The --force-with-lease flag is safer than --force because it checks if anyone else has pushed to the branch since your last fetch. If they have, it refuses to push and warns you.

Pull with rebase

You can make git pull automatically rebase instead of creating merge commits:

# One time
git pull --rebase origin main

# Or configure it permanently
git config --global pull.rebase true

This keeps your local branches linear without any extra effort.

05

Quick reference

CommandWhat it does
git rebase mainReplay current branch commits on top of main
git rebase --abortCancel rebase and return to the state before
git rebase --continueContinue rebase after resolving a conflict
git rebase --skipSkip the current commit (use with caution)
git pull --rebaseFetch and rebase instead of creating a merge commit
git push --force-with-leaseForce push safely after rebase