AI Hints
Git & GitHub · Lesson 40 of 144

Creating and Switching

Git branch and git switch.

⏱ 2 min read▶ Video at 43:20🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Two commands do most of the work with branches. Git branch creates and lists them. Git switch moves you from one branch to another.

Creating and Switching
From the video: Creating and Switching
  • git branch: create and list
  • git switch: move between them
  • That's most of it

Create, list, switch

Git branch on its own lists our branches. Just main, with a star, meaning we're on it. Now git branch dark creates a new branch. List again. Two branches, but the star is still on main. Creating a branch doesn't move you. To move, we type git switch dark. Git says switched to branch dark.

git
git branch
* main
git branch dark
git branch
  dark
* main
git switch dark
Switched to branch 'dark'

One step

Creating and switching is so common, there's a shortcut. Git switch, dash c, and a name. Dash c means create. It creates the branch and moves you onto it, in one step. Git status confirms we're on branch contact.

git
git switch -c contact
Switched to a new branch 'contact'
git status
On branch contact
nothing to commit, working tree clean

Summary

Git branch creates and lists branches. Git switch moves between them, and switch dash c does both at once. Next, how head follows you around.

Key takeaways
  • git branch <name>
  • git switch <name>
  • git switch -c <name>
Next up: HEAD and Branches — where you are