Skip to main content
Branching & Merging

git checkout / switch

Switch between branches or restore working tree files

Practical Example

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.

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.

Related Commands in Branching & Merging