Course contentsShow
← Data Science
Lesson 2006 of 2,14546. Version Control with GitFree lesson

Creating and Switching Branches

Using git branch to create new branches and git checkout or git switch to move between them.

Creating and Switching Branches

You'll learn: How to create new branches with git branch and move between them using git checkout or git switch.

Now that you understand why branches exist, it's time to actually create and navigate them. Think of this as building new railroad tracks and then choosing which track your train (your work) will run on.

Creating a New Branch

To create a new branch, use:

git branch feature-name

This creates a new branch pointer at your current commit, but doesn't switch to it yet. It's like building a new track parallel to where you are, but you're still on the original track.

To see all your branches:

git branch

The asterisk (*) marks your current branch.

Switching Between Branches

You have two options to switch branches:

Option 1: The classic way

git checkout feature-name

Option 2: The newer, clearer way

git switch feature-name

Both commands move you to the specified branch, updating your working directory to match that branch's latest commit. Git introduced git switch to make the intent clearer (since checkout does many things).

The Shortcut: Create and Switch in One Step

Instead of creating then switching separately, combine them:

git checkout -b new-feature
# or
git switch -c new-feature

The -b (branch) and -c (create) flags create the branch and switch to it immediately—the most common workflow.

What Happens When You Switch?

Git updates your working directory files to match the target branch's state. Any uncommitted changes come with you, so commit or stash work before switching if there are conflicts.

Key Takeaway: Use git branch to create branches, git switch (or git checkout) to move between them, and the -c or -b flags to do both at once—this is how you safely experiment in parallel workstreams.