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 featureNew branch, and you are on it.
git branch --show-currentfeature.
git switch mainBack to main by name.
git switch -And back again, without naming anything.
git branch --show-currentfeature. The dash means 'wherever I just came from'.
Your turn
Go back to the branch you were on before this one.
git switch
Try it in a real repository
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.