Rebasing

Topic 1 of 5 in Rewriting history6 min readBeginnerFree course

Overview

Merging is one way to combine branches. Rebasing is the other. Instead of joining two lines of history with a merge commit, rebase moves your branch: it takes your commits and replays them, one by one, on top of a new starting point. The result is a straight, tidy history — as if you’d started your work from the latest main all along.

git rebase replays your commits on a new base. Before: main has a1, 7d and b2; feature branched off at 7d and has e4 and f5. After git rebase main (on feature): the history is a1, 7d, b2, then e4’ and f5’ — new copies with new IDs — with main at b2 and feature at f5’. Result: a straight line, as if you had started your work from the latest main.

Rebasing a feature branch

Terminal
git switch feature/search
git rebase main
Text
Successfully rebased and updated refs/heads/feature/search.

What Git did:

  1. Found the commits on feature/search that aren’t on main (e4, f5).
  2. Temporarily set them aside and moved the branch to main’s tip.
  3. Re-applied each commit’s changes, in order, creating new commits (e4', f5').
  4. Moved feature/search to the last new commit.

Now main can absorb the feature with a fast-forward, no merge commit needed:

Terminal
git switch main
git merge --ff-only feature/search

Why the commits change

Remember that a commit’s ID depends on its parent. e4' has the same changes and message as e4, but a different parent (b2 instead of 7d), so it’s a different commit with a different ID. The commit dates are kept; the committer date is updated.

The original commits aren’t destroyed — no branch points to them, but the reflog does, and ORIG_HEAD points to where the branch was before the rebase. To undo a rebase you just finished:

Terminal
git reset --hard ORIG_HEAD

Conflicts during a rebase

Each replayed commit can conflict, just like a merge. Git stops at that commit:

Text
CONFLICT (content): Merge conflict in config.txt
error: could not apply 9b8c7d6... Enable search
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".

Resolve it exactly as in the previous lesson — edit, remove markers, git add — then:

Terminal
git rebase --continue     # carry on with the next commit
git rebase --skip         # drop this commit entirely (rarely what you want)
git rebase --abort        # give up and put everything back as it was

Because commits are replayed one at a time, you may resolve conflicts several times in one rebase — but each conflict is small and about one commit, which often makes it easier than one big merge conflict.

One thing to watch: during a rebase, the sides are reversed compared with a merge. “ours” (HEAD) is the branch you’re rebasing onto — main — and “theirs” is your commit being replayed. The markers label them, so read them carefully.

Pulling with rebase

When you pull and your local branch has new commits, a merge-based pull creates a “Merge branch ‘main’ of github.com…” commit. Rebasing your local commits on top of what you fetched avoids that noise:

Terminal
git pull --rebase

Many developers make this their default (Pushing and pulling explains the setting).

Rebase onto something else

--onto moves a branch to an arbitrary base — for example, when a branch was accidentally created from another feature branch instead of main:

Terminal
# feature/b was started from feature/a; move only b's own commits onto main
git rebase --onto main feature/a feature/b

The golden rule of rebasing

Never rebase commits that other people already have.

If you’ve pushed feature/search and a teammate has pulled it, your rebase gives the branch new commits with new IDs. Their copy still has the old ones. When they pull, Git sees two different histories with the same changes, and the result is duplicated commits and confusing conflicts.

Rebasing is safe when:

  • the commits are only on your machine, or
  • the branch is yours alone on GitHub — a feature branch nobody else is working on. After rebasing you’ll need to force-push it (git push --force-with-lease, covered in Pushing and pulling).

Never rebase main or any branch others build on.

Summary: rebase = “pretend I started this branch from here”. It produces clean, linear history for your own work — just keep it away from commits you’ve shared.

Exercise

Bring a feature branch up to date

feature/search was started from an old main. Since then, main has two new commits. Rebase the feature branch onto main so its two commits sit on top of the latest main, and then fast-forward main to include them. One of main’s new commits touches the same line of config.txt as the feature — resolve that conflict during the rebase by keeping both settings. Finish with a straight-line history of five commits.

Terminal
mkdir rebase && cd rebase && git init -b main
printf "debug=false\n" > config.txt && git add . && git commit -qm "Add config"

git switch -qc feature/search
echo "search" > search.js && git add . && git commit -qm "Add search"
printf "debug=false\nsearch=true\n" > config.txt && git commit -qam "Enable search"

git switch -q main
echo "# App" > README.md && git add . && git commit -qm "Add README"
printf "debug=false\ncache=true\n" > config.txt && git commit -qam "Enable cache"

# 1. Rebase feature/search onto main (expect one conflict)

# 2. Fast-forward main

# 3. Check the history

Summary

  • git rebase main replays your branch’s commits on top of the latest main, as if you had started there.
  • Rebased commits are new commits with new IDs; the originals are left behind (and in the reflog).
  • During a rebase conflict, fix the files, git add them and run git rebase --continue, or --abort to undo.
  • After rebasing, merging the branch into main is a fast-forward, so history stays linear.
  • Only rebase commits that nobody else has — never rebase shared history.