Lessons · Git · replaying commits somewhere else
Replaying your commits on a new base
From a feature branch, git rebase main lifts your commits off where they were and lays them down again on top of main's newest commit. The changes are the same; the commits themselves are new, with new hashes.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
It brings your branch up to date with main without adding a merge commit. The history stays one column instead of splitting in two and joining back, which is easier to read later and easier to search when you are hunting the commit that broke something.
How to think about it
Rebase what is yours and unshared. Resolve conflicts one commit at a time (rebase --continue), and never rebase commits others already have. -i to tidy the last few before a pull request.
Worked example
git switch featureYour branch, started from an older main.
git log --oneline --graph --all -5BEFORE. Two lines side by side: yours, and main's newer commit, splitting apart at the commit you branched from. --all is what makes main visible here; without it you only ever see your own commits and nothing looks split.
git rebase mainTakes your commits off and lays them down again on top of main's newest: same changes, new hashes, no merge commit.
git log --oneline --graph --all -5AFTER. One column. Your commits now sit directly above main's newest, so nothing splits and nothing has to join back.
git rebase -i HEAD~3Interactive: reorder, squash or reword the last three before sharing.
Your turn
Move your branch onto the latest main.
git rebase
Try it in a real repository
The trap
Never rebase commits others already have. Rebasing rewrites them, and everyone holding the old hashes has to reconcile.