# Creating & Switching Branches

> How to use `git branch` to create pointers and `git switch` to move your HEAD cursor.

Subject: Version Control | Lesson 9 of 29 | 5 min read
Interactive lesson: https://codewithanimation.in/git/git-branch-and-switch
Previous: [What is a Branch?](https://codewithanimation.in/git/what-is-a-branch.md) | Next: [Merging Basics](https://codewithanimation.in/git/git-merge.md)

## Creating and Switching Branches

`git branch <name>` is the command used to create a new branch. In Git, creating a branch simply creates a new text file containing the hash of the current commitâ€”it's just a new pointer.

`git switch <name>` (or `git checkout`) is used to change the currently active branch. `git switch` was recently introduced to explicitly handle changing branches without the confusing secondary functions (like reverting files) that `git checkout` possesses.

## Why it matters

Web developers often work on multiple things simultaneously (e.g., building a new navigation bar, fixing a footer bug).

Branches allow a developer to create a safe 'sandbox' to work on a new feature in isolation without breaking the stable `main` branch. Once the feature is complete and tested, the branch can be merged back into `main`.

## How it works

Git tracks your current active branch using a special pointer called `HEAD`. When you run `git switch <branch-name>`, Git moves the `HEAD` pointer to attach it to the specified branch.

Git then magically updates the files in your working directory to match the exact snapshot of the commit that branch points to.

## Common mistake: Forgetting to switch before coding

The #1 common mistake is starting to write code for a new feature while still on the `main` branch, forgetting to create and switch to a feature branch first.

This requires carefully stashing or moving uncommitted changes to a new branch later, which often leads to confusion or accidentally committing unfinished code directly to `main`.

## Tip: Create and switch in one step

Use `git switch -c <branch-name>` (or `git checkout -b <branch-name>`) to create a branch and immediately switch to it in a single step.

This builds a mental model of 'branching out and stepping into' the new timeline simultaneously, preventing the common mistake of coding on the wrong branch.

## Try it: Create and switch branches

Create a branch at the current commit, then switch branches and inspect HEAD.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-branch-and-switch

## Check yourself

### Question 1

What does the `git switch` command actually do under the hood?

- A. It merges the specified branch into your current branch.
- B. It creates a completely separate folder on your computer with a copy of all your files.
- C. It moves the `HEAD` pointer to the specified branch and updates your working directory files to match.
- D. It deletes the branch you were previously working on to save space.

Correct answer: C

When you switch branches, Git moves the special `HEAD` pointer (which tracks where you are) to the new branch pointer, and magically updates your project folder so the files match that branch's exact state.

- Why not A: This is the job of the `git merge` command, not `git switch`.
- Why not B: Git doesn't copy folders for branches; it tracks changes internally and swaps out the files in your single working directory.
- Why not D: Switching branches leaves your old branch safely intact. Deleting requires `git branch -d`.

### Question 2

Why did Git introduce the `git switch` command when `git checkout` already existed?

- A. Because `git checkout` was completely removed from the Git system.
- B. Because `git checkout` was overloaded with too many different responsibilities, making it confusing for beginners.
- C. Because `git switch` runs significantly faster on large repositories.
- D. Because `git checkout` was unable to create new branches.

Correct answer: B

`git checkout` could switch branches, but it was also used to discard file changes and view old commits. To make Git safer and easier to learn, `git switch` was created solely for changing branches.

- Why not A: `git checkout` is still fully functional and widely used in older tutorials.
- Why not C: The performance is identical; the change was made purely for better user experience.
- Why not D: Developers have successfully used `git checkout -b` to create branches for many years.

### Question 3

If you are currently on the `main` branch and run the command `git branch new-layout`, what happens?

- A. You are immediately moved to the `new-layout` branch and can start coding.
- B. Git copies all your commits to the remote GitHub repository under a new name.
- C. Git gives an error warning you to commit your current changes first.
- D. A new branch pointer named `new-layout` is created at your current commit, but you remain on the `main` branch.

Correct answer: D

The `git branch <name>` command is strictly for creation. It stamps a new sticky note (pointer) on your current commit. You must explicitly tell Git to move your `HEAD` pointer to it using `git switch` if you want to work there.

- Why not A: `git branch` only creates the pointer. You must run `git switch new-layout` to actually move to it.
- Why not B: Branch creation is completely local. You must use `git push` to send the branch to a remote server.
- Why not C: You can create a new branch pointer at any time, even with uncommitted changes in your working directory.

## Remember this

- `git branch <name>` creates a new branch pointer but DOES NOT switch you to it.
- `git switch <name>` moves HEAD to a branch and updates your working directory.
- Use `git switch -c <name>` to create and switch in one command.
