Loading lessons...
Switch and Checkout
Switch and Checkout
Creating a branch is not enough. You also need to switch to it to start working there.
Switch to a branch
git checkout feature-login
This is the classic command. It moves you onto the feature-login branch, and your files update to match that branch.
The modern way
git switch feature-login
git switch was added to make switching clearer. It only switches branches, nothing else.
Create and switch in one step
git switch -c new-feature
The -c flag creates the branch and switches to it in one command.
The same with checkout
git checkout -b new-feature
-b does the same as -c.
Which should you use?
Both work. Modern guidance is:
- Use
git switchto change branches. - Use
git checkoutfor older muscle memory or restoring files.
TL;DR
git checkout branchorgit switch branchmoves to a branch.git switch -c namecreates and switches.git checkout -b nameis the older form.- Your working files update to match the branch you switch to.