Hone

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 HEAD
The commit you are about to rebase. Note the first few characters.
git branch feature-backup
A second name. Nothing has moved and nothing was copied.
git rebase main
The rebase builds new commits with new hashes.
git rev-parse HEAD
A different hash: this is a copy, not the original.
git rev-parse feature-backup
And the original is still named, still reachable.
git log --oneline --all
Both, side by side, until you decide the rebase was right.

Your turn

Give the current commit a second name before rebasing.

git  feature-backup

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.

Practise a branch before anything risky on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.