Git Foundations / Rebase Unpublished Work

Rebase onto an updated branch

Lesson 20 of 33. Open to read. No account needed.

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 main into your branch, which adds a merge commit.
  • Rebase your branch onto main, which moves your commits so they start from main's newest commit.

What Rebase Does

Before: desserts split from main at commit M, main has since gained H, and desserts has commits D1 and D2. After git rebase main: new commits D1-prime and D2-prime, making the same changes, sit on top of H, and desserts points at D2-prime. The original D1 and D2 are no longer on any branch, and the history is a straight line.

On the desserts branch, git rebase main:

  1. Finds the commits on desserts that main does not have, D1 and D2.
  2. Replays them one at a time on top of main's tip, H, much like cherry-picking each one.
  3. Moves desserts to 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 branchRebase the branch onto main
Existing commitsKept with their IDsReplaced by copies with new IDs
History shapeShows the branches joiningA straight line
New commitsOne merge commitNone
Safe for shared commitsYesNo

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.

  1. 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
    
  2. Look at the history, and note the IDs of the two desserts commits:

    git log --oneline --graph --all
    
    * 9d6c6e8 (HEAD -> desserts) Add lemon tart
    * 3d7dd12 Add desserts page
    | * 423c77a (main) Add opening hours
    |/
    * 31d2191 Add menu
    
  3. First, bring main in with a merge:

    git merge --no-edit main
    git log --oneline --graph --all
    

    The graph shows Merge branch 'main' into desserts joining the two lines.

  4. Undo that merge, which exists only on your computer:

    git reset --hard ORIG_HEAD
    

    Git prints HEAD is now at with the "Add lemon tart" ID from step 2.

  5. Now rebase instead:

    git rebase main
    git log --oneline --graph --all
    

    Git 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 menu
    

    The two desserts commits have the same messages but new IDs, and they now come after "Add opening hours".

  6. Check that the branch still makes the same change:

    git diff main desserts
    

    The only difference from main is the new desserts.md with its four lines, exactly what the two commits added before the rebase.

  7. Merge the branch into main:

    git switch main
    git merge desserts
    git log --oneline --graph --all
    

    Git reports Fast-forward: after the rebase, main has nothing that desserts lacks.

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 seeLikely causeNext step
Current branch desserts is up to date.The branch already starts from main's tipNothing to do
CONFLICT during the rebaseA replayed commit changes lines that main also changedThe next lesson resolves this; git rebase --abort returns to the start
could not apply and a stopped rebaseSame as abovegit status shows the state; git rebase --abort
The merge in step 7 made a merge commitThe rebase did not finish, or main changed againCheck git log --oneline --graph --all
  1. 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

  1. After a rebase, the branch's commits have new IDs. Why?
  2. You pushed desserts to a shared repository yesterday, and a teammate based work on it. Today you rebase it onto main and 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

  1. A commit's ID depends on its parent. Rebasing gives each replayed commit a new parent, the tip of main or the previous replayed commit, so each one is a new commit with a new ID, even though its changes and message are the same.
  2. 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 merging main into it.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy