Lessons · Git · the commit went on the wrong branch
The commit went on the wrong branch
Nothing has to move. Point a new branch at the commit, then move the branch you were on back one, and the commit is where it should have been.
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 is the commonest ten-second mistake in git, and the fix reads as frightening until you notice that a branch is only a name.
How to think about it
Name it first, move second. In that order the reset is bookkeeping; the other way round it is a rescue.
Worked example
git branch --show-currentmain. Which is the problem.
git log --onelineAnd the commit that should not be here.
git branch featureA second name for exactly this commit. Nothing has moved.
git reset --hard HEAD~1Now main goes back, and the commit is still named.
git log --onelinemain is clean.
git log --oneline featureAnd the work is on feature, where it was meant to go.
Your turn
Give the current commit a second name before moving main off it.
git feature
Try it in a real repository
The trap
Resetting first and creating the branch afterwards. It still works through the reflog, and there is no reason to need the reflog for this.
Practise the commit went on the wrong branch on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.