Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Git

    Rebasing

    git pull --rebase — put your local unpushed commits on top of what's new on the server, instead of creating a

    Chapter 6 of 114 min read

    The Plain Picture#

    You wrote three pages of a group report on Monday, based on Monday's draft. By Wednesday, others have added pages. Instead of stapling your pages on with a note saying "these were written against Monday's version", you rewrite your three pages on top of Wednesday's draft, as if you'd started today. The final report reads as one straight story.

    The Real Name#

    Rebasing takes the commits on your branch and replays them, one by one, on top of another commit — creating new commits with new hashes (Ch 3: a different parent means a different hash).

      before:                                   after `git rebase main` (while on feature):
    
            A───B───C  main                           A───B───C  main
                 \                                             \
                  D───E  feature                                D'───E'  feature
    
      D and E are replayed on top of C as new commits D' and E'

    Then main can fast-forward (Ch 5) to include them — giving a perfectly linear history with no merge commits.

    Under the Hood — merge vs rebase#

    MergeRebase
    Historyshows what really happened, including parallel worka straight line — easier to read
    Createsa merge commit (two parents)new copies of your commits (new hashes)
    Rewrites existing commitsnoyes
    Conflictsresolved once, in the merge commitmay need resolving per replayed commit
    Safe on shared branchesalwaysonly on commits nobody else has

    The golden rule of rebasing: never rebase commits that others already have. If you rebase commits you've already pushed to a shared branch (Ch 7), your teammates' copies still point at the old commits, and their history and yours now disagree — a painful mess. Rebase your own, not-yet-shared work.

    Common uses:

    • git pull --rebase — put your local unpushed commits on top of what's new on the server, instead of creating a merge commit for every pull.

    • git rebase -i main — interactive rebase: reorder, reword, or squash (combine) your commits into a tidy set before opening a pull request (Ch 8). It opens an editor listing your commits with a word (pick, squash, reword, drop) in front of each.

    If a rebase goes wrong midway: git rebase --abort returns to exactly how things were.

    Check yourself

    4 questions
    1

    What does rebasing do to your commits?

    Show answer

    Replays them on top of another commit, creating new commits with new hashes.

    2

    What's the golden rule of rebasing, and why?

    Show answer

    Never rebase commits others already have — it rewrites history they depend on, and their copies diverge from yours.

    3

    Merge vs rebase — one advantage of each.

    Show answer

    Merge: preserves true history and never rewrites commits. Rebase: clean, linear, easy-to-read history.

    4

    What does squashing mean?

    Show answer

    Combining several commits into one.