Hone

Lessons · Git · returning to the branch you were on

Back to the branch you were on

git switch - goes to the previous branch, the way cd - goes to the previous directory. And switch exists because checkout did two unrelated jobs.

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

Half of being lost in a repository is not being sure which branch you are on, and the fastest cure is a command that does not need you to remember a name.

How to think about it

switch for branches, restore for files. The old checkout still does both, which is exactly why it was split.

Worked example

git switch -c feature
New branch, and you are on it.
git branch --show-current
feature.
git switch main
Back to main by name.
git switch -
And back again, without naming anything.
git branch --show-current
feature. The dash means 'wherever I just came from'.

Your turn

Go back to the branch you were on before this one.

git switch 

The trap

Using `git checkout -- file` from memory when you meant to switch branches. Two dashes and a filename is the destructive half of the old command.

Practise returning to the branch you were on on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.