Skip to main content
Back to command reference
Branching & Merging

git checkout / switch

Switch between branches or restore working tree files

Practical example

bash
git checkout main                  # switch to existing branch
git checkout -b fix-bug             # create and switch to new branch
git switch -c hotfix                # modern way (Git 2.23+): create + switch

Command overview & how it works

Changes your current HEAD to point to a different branch or commit, updating the working directory to match. Git checkout is the traditional Swiss Army knife command — it can switch branches (git checkout main), create and switch branches (git checkout -b feature-login), and restore files from previous states (git checkout HEAD -- file.ts). In modern Git (2.23+), the command has been split: git switch handles branch switching with a cleaner interface (git switch other-branch, git switch -c new-branch), and git restore handles file restoration. Use git checkout -b <branch> <start-point> to create a branch from a specific commit or tag. Git prevents switching if you have uncommitted changes that would conflict with the target branch — use git stash to temporarily save them.

Chapter 04 · Branching & Merging

Resolve conflicts and master branch mechanics

Understand 3-way diffs, conflict marker anatomy, and rebasing playbooks in the 97-page offline course — ₹499.

Learn more

Practical tips

  • Prefer git switch for branch switching and git restore for file restores — the modern, focused replacements for git checkout.
  • Uncommitted changes can block a branch switch; stash them first with git stash and pop them once you switch back.
  • Create a branch from any point with git switch -c new-branch <start-point>, for example a tag or a specific commit hash.

Deep-dive guides & tutorials

In-depth walkthroughs and practical scenarios related to git checkout / switch

All guides →

Related commands

More from Branching & Merging