Catching Up With main
While you work on a feature branch, main moves on: other work is merged into
it. Before you finish, you often want your branch to include those changes, to
test against them or to make the final merge simple. There are two ways to
bring them in:
- Merge
maininto your branch, which adds a merge commit. - Rebase your branch onto
main, which moves your commits so they start frommain's newest commit.
What Rebase Does

On the desserts branch, git rebase main:
- Finds the commits on
dessertsthatmaindoes not have, D1 and D2. - Replays them one at a time on top of
main's tip, H, much like cherry-picking each one. - Moves
dessertsto the last replayed commit.
The replayed commits, D1' and D2', make the same changes with the same
messages, but they have new parents, so they get new IDs. The originals are
no longer on any branch. The history becomes a straight line, as if you had
started the feature after H, and merging desserts into main afterwards is a
fast-forward.
Git reports Successfully rebased and updated refs/heads/desserts. when it is
done.
Merge or Rebase?
Merge main into the branch | Rebase the branch onto main | |
|---|---|---|
| Existing commits | Kept with their IDs | Replaced by copies with new IDs |
| History shape | Shows the branches joining | A straight line |
| New commits | One merge commit | None |
| Safe for shared commits | Yes | No |
Both give you the same files. The difference is the history: merging records what really happened, while rebasing gives a cleaner line. Teams often agree on one style.
Never Rebase Shared Commits
Rebasing replaces commits. If others already have the originals, for example because you shared the branch, their history and yours now disagree. Anyone who built on the old commits is forced to repair their own history, and the old and new versions of each commit can end up in the project side by side.
Git also refuses an ordinary push that would replace commits on a shared
branch: it reports [rejected] with (non-fast-forward). The remotes module
covers pushing in detail.
So rebase only commits that exist only on your computer. Once a branch is
shared, merge main into it instead.
Exercise: Rebase a Feature Branch onto an Updated main
A café's desserts branch has two commits. Meanwhile main gained an
opening-hours page. You will try a merge, undo it, then rebase and compare.
Starting state: Git configured as in the configuration lesson.
-
Run the setup:
mkdir -p ~/git-practice/rebase-practice cd ~/git-practice/rebase-practice git init -b main cat > menu.md <<'EOF' # Menu Tea Coffee EOF git add menu.md git commit -m "Add menu" git switch -c desserts cat > desserts.md <<'EOF' # Desserts Carrot cake EOF git add desserts.md git commit -m "Add desserts page" cat >> desserts.md <<'EOF' Lemon tart EOF git commit -am "Add lemon tart" git switch main cat > hours.md <<'EOF' # Opening hours Daily, 8:00 to 18:00 EOF git add hours.md git commit -m "Add opening hours" git switch desserts -
Look at the history, and note the IDs of the two
dessertscommits:git log --oneline --graph --all* 9d6c6e8 (HEAD -> desserts) Add lemon tart * 3d7dd12 Add desserts page | * 423c77a (main) Add opening hours |/ * 31d2191 Add menu -
First, bring
mainin with a merge:git merge --no-edit main git log --oneline --graph --allThe graph shows
Merge branch 'main' into dessertsjoining the two lines. -
Undo that merge, which exists only on your computer:
git reset --hard ORIG_HEADGit prints
HEAD is now atwith the "Add lemon tart" ID from step 2. -
Now rebase instead:
git rebase main git log --oneline --graph --allGit reports
Successfully rebased and updated refs/heads/desserts.The graph is a single line:* 1425bfa (HEAD -> desserts) Add lemon tart * 045b4e7 Add desserts page * 423c77a (main) Add opening hours * 31d2191 Add menuThe two
dessertscommits have the same messages but new IDs, and they now come after "Add opening hours". -
Check that the branch still makes the same change:
git diff main dessertsThe only difference from
mainis the newdesserts.mdwith its four lines, exactly what the two commits added before the rebase. -
Merge the branch into
main:git switch main git merge desserts git log --oneline --graph --allGit reports
Fast-forward: after the rebase,mainhas nothing thatdessertslacks.
Success: the graph is linear, the desserts commits have new IDs, and the
desserts page contains the same lines as before.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
Current branch desserts is up to date. | The branch already starts from main's tip | Nothing to do |
CONFLICT during the rebase | A replayed commit changes lines that main also changed | The next lesson resolves this; git rebase --abort returns to the start |
could not apply and a stopped rebase | Same as above | git status shows the state; git rebase --abort |
| The merge in step 7 made a merge commit | The rebase did not finish, or main changed again | Check git log --oneline --graph --all |
-
Clean up:
cd ~/git-practice ls rm -rf rebase-practice ls
Independent task: before cleaning up, main moves on again and you add one
more dessert:
cat >> menu.md <<'EOF'
Hot chocolate
EOF
git commit -am "Add hot chocolate"
git switch desserts
cat >> desserts.md <<'EOF'
Apple pie
EOF
git commit -am "Add apple pie"
Bring desserts up to date with main without a merge commit, then bring the
apple pie into main. Before merging, predict whether it will fast-forward.
Check Your Understanding
- After a rebase, the branch's commits have new IDs. Why?
- You pushed
dessertsto a shared repository yesterday, and a teammate based work on it. Today you rebase it ontomainand push. What happens, and why is rebasing it a bad idea?
Answer and Reasoning
Independent task
git rebase main
git log --oneline --graph --all
git switch main
git merge desserts
On desserts, git rebase main replays "Add apple pie" on top of "Add hot
chocolate". The merge then fast-forwards, because main has nothing that
desserts lacks, and the graph remains a single line.
Understanding check
- A commit's ID depends on its parent. Rebasing gives each replayed commit a
new parent, the tip of
mainor the previous replayed commit, so each one is a new commit with a new ID, even though its changes and message are the same. - The push is rejected as
non-fast-forward, because the shared branch has commits that your rebased branch replaced. Forcing it through would remove the originals your teammate built on, leaving them to repair their history and risking duplicated commits. A shared branch should be updated by mergingmaininto it.