Lessons · Git · a branch before anything risky
A branch before anything risky
A branch is a small file holding one commit hash. Creating one copies nothing and protects everything it points at from becoming unreachable.
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
A rebase does not move your commits, it copies them, and the originals become unreachable when the branch moves. A second name means they never do.
How to think about it
Before a rebase, a reset or anything you have not done before: git branch something-backup. Delete it afterwards if you want to.
Worked example
git rev-parse HEADThe commit you are about to rebase. Note the first few characters.
git branch feature-backupA second name. Nothing has moved and nothing was copied.
git rebase mainThe rebase builds new commits with new hashes.
git rev-parse HEADA different hash: this is a copy, not the original.
git rev-parse feature-backupAnd the original is still named, still reachable.
git log --oneline --allBoth, side by side, until you decide the rebase was right.
Your turn
Give the current commit a second name before rebasing.
git feature-backup
Try it in a real repository
The trap
Thinking of a branch as a copy of the code. It is a pointer, which is why 'branch freely' is advice rather than extravagance.