Hone

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 feature
Your branch, started from an older main.
git log --oneline --graph --all -5
BEFORE. 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 main
Takes 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 -5
AFTER. One column. Your commits now sit directly above main's newest, so nothing splits and nothing has to join back.
git rebase -i HEAD~3
Interactive: reorder, squash or reword the last three before sharing.

Your turn

Move your branch onto the latest main.

git rebase 

The trap

Never rebase commits others already have. Rebasing rewrites them, and everyone holding the old hashes has to reconcile.

Practise replaying commits somewhere else on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.