Menu
CoddyTech

Switching Branches

Part of the Version Control section of Coddy's Terminal journey. Lesson 30 of 58.

Creating a branch and moving to it are two separate actions. The modern command for switching is git switch:

git switch feature-login

If you want to create and switch in one step, use the -c flag ("create"):

git switch -c feature-login

This is the most common workflow when starting new work.

You may also see git checkout in older tutorials. It does the same thing and still works:

git checkout feature-login
git checkout -b feature-login   # create and switch

To find out which branch you are on right now, run git branch --show-current:

git branch --show-current
feature-login
challenge icon

Challenge

Beginner

Set up the repository and make one commit on main, then create and switch to a new branch called experiment in a single command, and print the current branch name.

Every run starts from a fresh copy of the folder, and the whole script in the editor runs again from the top. Nothing is committed for you: begin with git init -q -b main, set git config user.name and git config user.email, and commit the starting file. Keep those lines in the editor. If you clear them you will see fatal: not a git repository.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Version Control

Practice on your own: Terminal playground