Course contentsShow
← Software Engineering
Lesson 56 of 1,9332. Version Control with GitFree lesson

Creating and Switching Branches

Master creating new branches with git branch and switching between branches using git checkout or git switch.

Creating and Switching Branches

What you'll learn: How to create new branches and move between them in Git.

The Core Idea

Now that you understand what branches are (parallel versions of your code), it's time to actually create and use them. Git gives you simple commands to make new branches and switch your working directory between them.

Creating a branch is like adding a new bookmark to your project's timeline. The branch doesn't change your files immediately—it just creates a new pointer to your current commit. Switching branches is like choosing which bookmark to follow: Git updates your working directory to match that branch's latest state.

Creating a Branch

Use git branch followed by a name:

git branch feature-login

This creates a new branch called feature-login pointing to your current commit. Important: This doesn't switch to the new branch—you're still on your previous branch (usually main).

To see all branches:

git branch

The asterisk (*) shows your current branch.

Switching Branches

Use git checkout (older way) or git switch (newer, clearer):

git checkout feature-login

or

git switch feature-login

Now your working directory reflects the feature-login branch. Any commits you make will advance this branch, not main.

Shortcut: Create and Switch in One Step

git checkout -b feature-payment

or

git switch -c feature-payment

This creates feature-payment and immediately switches to it.

Key Takeaway: Use git branch <name> to create branches and git checkout <name> or git switch <name> to move between them—this lets you work on multiple features without mixing changes.