Rebasing
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.
Rebasing a feature branch
git switch feature/search
git rebase mainSuccessfully rebased and updated refs/heads/feature/search.What Git did:
- Found the commits on
feature/searchthat aren’t onmain(e4,f5). - Temporarily set them aside and moved the branch to
main’s tip. - Re-applied each commit’s changes, in order, creating new commits (
e4',f5'). - Moved
feature/searchto the last new commit.
Now main can absorb the feature with a fast-forward, no merge commit needed:
git switch main
git merge --ff-only feature/searchWhy 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:
git reset --hard ORIG_HEADConflicts during a rebase
Each replayed commit can conflict, just like a merge. Git stops at that commit:
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:
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 wasBecause 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:
git pull --rebaseMany 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:
# feature/b was started from feature/a; move only b's own commits onto main
git rebase --onto main feature/a feature/bThe 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.
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.
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 historymkdir 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)
git switch feature/search
git rebase main || true # CONFLICT in config.txt while applying "Enable search"
git status # "interactive rebase in progress"… shows config.txt
printf "debug=false\ncache=true\nsearch=true\n" > config.txt
git add config.txt
GIT_EDITOR=true git rebase --continue
# 2. Fast-forward main
git switch main
git merge --ff-only feature/search
# 3. Check the history
git log --oneline --graph # five commits in one straight lineSummary
git rebase mainreplays your branch’s commits on top of the latestmain, 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 addthem and rungit rebase --continue, or--abortto undo. - After rebasing, merging the branch into
mainis a fast-forward, so history stays linear. - Only rebase commits that nobody else has — never rebase shared history.