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-loginIf you want to create and switch in one step, use the -c flag ("create"):
git switch -c feature-loginThis 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 switchTo find out which branch you are on right now, run git branch --show-current:
git branch --show-current
feature-loginChallenge
BeginnerThe repo has one commit on main. Create and switch to a new branch called experiment in a single command, then print the current branch name.
Cheat sheet
Switch to an existing branch:
git switch feature-loginCreate and switch to a new branch in one step with -c:
git switch -c feature-loginCheck which branch you are currently on:
git branch --show-currentOlder equivalent using git checkout:
git checkout -b feature-loginTry it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Version Control
7Branching
What Is A BranchCreating A BranchSwitching BranchesCommitting On A BranchDeleting A BranchRecap - Branch Play2Getting Started
Initialize A RepositoryThe .git FolderConfigure Your IdentityGit StatusRecap - First Repo8Merging
What Is A MergeFast-Forward MergeThree-Way MergeMerge ConflictsResolve A ConflictRecap - Merge Master11Feature Branch Project
Project OverviewInitialize Main3Tracking Changes
The Staging AreaGit AddGit CommitModifying A Tracked FileGit LogRecap - First Commits6Recipe Site Project
Project OverviewInitialize And Ignore