Course contentsShow
← Computer Science
Lesson 2183 of 2,87249. Version Control with GitFree lesson

Creating and Switching Branches

Master the git branch and git checkout commands to create new branches and move between them in your repository.

Creating and Switching Branches

You'll learn how to create new branches and navigate between them using git branch and git checkout.

Core Concepts

Now that you understand what branches are, it's time to create and move between them. Creating a branch is like adding a new bookmark in your project's history—you're marking a point where development can diverge into parallel workstreams.

Creating a Branch

Use git branch to create a new branch:

git branch feature-login

This creates a branch named feature-login pointing to your current commit. However, you're still on your original branch—creation doesn't automatically switch you over.

To see all branches:

git branch

The asterisk (*) shows which branch you're currently on.

Switching Branches

To move to a different branch, use git checkout:

git checkout feature-login

Now your working directory reflects the state of feature-login, and new commits will extend this branch instead of your previous one.

The Shortcut

You can combine creation and switching in one command:

git checkout -b feature-profile

This creates feature-profile and immediately switches to it—a huge time-saver in daily workflows.

Why This Matters

Think of branches as parallel universes for your code. You might work on a risky experiment in one branch while keeping your stable code safe in another. Switching branches is instantaneous—Git simply updates your files to match the branch's latest commit.

Key Takeaway: Use git branch <name> to create branches, git checkout <name> to switch between them, or combine both with git checkout -b <name> to create and switch in one step.