Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

GIT
Git & GitHub
0/10 completed
Beginner
What is version control?Your first commitsBranchesGitHub and remotes
Intermediate
Merging and conflictsUndoing changesPull requests and code reviewRebasing
Advanced
Git internals
Pro
Git workflows for teams
Tracks / Git & GitHub / Intermediate

Rebasing

Rebasing is the other way to integrate changes from one branch into another. Instead of creating a merge commit, it replays your commits on top of the other branch, as if you had started your work from its latest commit. The result is a clean, linear history.

Merge vs rebase

Starting point: you're on feature, and main has moved on.

          C ─ D          feature
         /
A ─ B ─ E ─ F            main

Merge (git merge main while on feature) keeps the real shape of history and adds a merge commit M:

          C ─ D ─ M      feature
         /       /
A ─ B ─ E ─ F ──┘        main

Rebase (git rebase main while on feature) re-creates C and D as new commits C' and D' on top of F:

A ─ B ─ E ─ F ─ C' ─ D'  feature
              ↑
             main

Try it

Git terminal (simulator) · editable

Look at the graph: feature is now a straight line on top of main, and its commits have new ids (they're new commits). Now main can fast-forward:

bash
git switch main
git merge feature

Type those into the terminal above and watch main slide forward.

The golden rule of rebasing

🚫 Never rebase commits that other people have already based work on (anything pushed to a shared branch like main).

Rebasing rewrites history: the old commits are replaced with new ones. Anyone who had the old commits now has a conflicting history. Rebasing your own local or feature branch is fine. If you've already pushed it, you'll need git push --force-with-lease.

When to use which

Use rebase to… Use merge to…
Update your feature branch with the latest main Integrate a finished feature into main (often via a PR)
Keep history linear and readable Preserve exactly what happened and when
Clean up your own commits before a PR Work with shared branches

A common team convention: rebase your feature branch on main while working; merge (or squash) the PR into main.

bash
git switch feature
git fetch origin
git rebase origin/main      # or: git pull --rebase origin main

Many developers set pull to rebase by default:

bash
git config --global pull.rebase true

Rebase conflicts

Because each commit is replayed one by one, you may get conflicts per commit:

bash
git rebase main
# CONFLICT (content): Merge conflict in app.js
# fix the file, then:
git add app.js
git rebase --continue      # move on to the next commit
# or give up and go back to how things were:
git rebase --abort

(The simulator aborts on rebase conflicts to keep things simple. Practise this flow in a real terminal.)

Interactive rebase: polishing your commits

git rebase -i lets you edit your history before sharing it: squash "fix typo" commits, reword messages, reorder or drop commits.

bash
git rebase -i HEAD~4

Your editor opens with:

pick a1b2c3d Add search API
pick d4e5f6a Fix typo
pick 9f8e7d6 Add search UI
pick 1a2b3c4 WIP

# Commands: p(ick), r(eword), e(dit), s(quash), f(ixup), d(rop)

Change it to:

pick a1b2c3d Add search API
fixup d4e5f6a Fix typo          ← melt into the previous commit, discard its message
pick 9f8e7d6 Add search UI
squash 1a2b3c4 WIP              ← combine with the previous commit, edit the message

Save and close, and Git rewrites the commits. Pros use this to turn messy work-in-progress into a clean, reviewable story.

bash
# Even faster: mark a fix for an earlier commit, then auto-squash it
git commit --fixup a1b2c3d
git rebase -i --autosquash main

Practice

  1. Create main with 2 commits. Branch feature and add 2 commits. Go back to main and add 1 more commit.
  2. Rebase feature onto main and confirm in the graph that the history is linear.
  3. Fast-forward main to feature.
Git terminal (simulator) · editable
✎ Check your understanding

1.What does git rebase main do while you're on feature?

2.Which is the golden rule of rebasing?

← Pull requests and code review