Git without fear · Chapter 17 of 28

Rebase onto main

By the end of this chapter you can move your branch on top of the latest main as a straight line, and say why that is only safe for commits nobody else has. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Branches
  • 4 tasks
  • Git
  • In the all labs plan

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.

Why you need this

Your order-tokens branch gives every order a token number, 1 to 99 and round again. While you worked on it, Ravi added two commits to main: the new cutting chai price and the opening hours. Kabir will review your branch only once it sits on top of today's main, as a straight line he can read from top to bottom.

The Git you know would merge main into your branch. Bunty suggests exactly that, and it works, but it leaves a "Merge branch 'main' into order-tokens" commit in the middle of your feature, and one more every time main moves. Kabir does not want that knot today.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Move order-tokens on top of main

    Move order-tokens so it starts from main's last commit.

    1. Run git log --oneline --graph --all. main has two commits that order-tokens does not have.

    2. Note the hash of Add token numbers now. Task 4 needs it.

    3. Stay on order-tokens. Do not switch to main.

    4. Rebase order-tokens onto main with git rebase.

    To pass: order-tokens contains main's last commit, "Add the opening hours". main does not change: it keeps exactly its four commits, Add the menu, Start the order list, Raise cutting chai to 30 and Add the opening hours.

  2. Task 2

    Keep a straight line, with no merge commit

    order-tokens must be one straight line, with no merge commit.

    1. Use a rebase. Do not merge main into order-tokens.

    2. Run git log --oneline to see the commits on order-tokens.

    3. Made a merge by mistake? Run lab reset and use a rebase.

    To pass: order-tokens has no merge commit. Its commits, newest first, are exactly: Start the tokens again after 99, Add token numbers, then main's four commits. Each of your two commits is there only once.

  3. Task 3

    Keep the new prices and your tokens.js

    After the rebase, order-tokens has main's changes and your own.

    1. Stay on order-tokens. Run cat menu.txt and cat hours.txt.

    2. Run cat tokens.js. It still starts again at 1 after 99.

    To pass: on order-tokens, menu.txt has Cutting chai 30, hours.txt has Open 7 am to 11 pm, and tokens.js is your latest version, the one that starts again after 99.

  4. Task 4 · interview question

    Interview: why not rebase shared commits?

    Interview question: why should you never rebase commits that other people already have?

    1. Compare the hash of Add token numbers before and after the rebase.

    2. Write one line in your own words: what does a rebase do to the commits it moves?

    3. Save it in answer.txt, in the chapter folder.

    To pass: answer.txt says what happens to the moved commits and their hashes.

What you will learn

  • Merge or rebase: two ways to bring main's new commits into your branch, and the history each one leaves.
  • git rebase: replays your commits, one by one, on top of another branch.
  • New commits, new hashes: why a rebase copies commits instead of moving them.
  • The golden rule: rebase only commits nobody else has.

More in Branches

Build "Rebase onto main" yourself

Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.