gitpractice.dev

brewly — Ch.4 · Committed on the wrong branch

Exercise 4 of 9

Before you start

A branch is just a name

Your commit is on main, but should be on its own branch. First give it a second name, then move main back.

git branch <name> creates a branch at the commit you are on without switching to it.

Then git reset --hard HEAD~1 moves main back. The commit stays safe, because the new branch still points at it. Check git status first: a hard reset overwrites uncommitted changes.

git status
make sure nothing is uncommitted
git branch <name>
name the current commit
git reset --hard HEAD~1
move the current branch one commit back
git switch <name>
continue on the new branch
git log --oneline --graph --all
check where the branches point
You can reopen this any time with “Lesson”.

Continue on a desktop

Typing git on a small screen is no fun. Open this page on a laptop or desktop to practise.

Back to the path