git checkout <branchname>

Switch to a different branch

Switching branches lets you move the HEAD pointer to a different line of development by updating your working copy and index.

The parameter <branchname> tells Git which branch to switch to; this command updates the HEAD reference, replaces files in your working directory with their versions on the target branch, and updates the staging area to match.

Variations include using git checkout -b <new-branch> to create and switch to a branch at once, replacing it with git switch <branchname> or git switch -c <new-branch> as modern alternatives, appending --track origin/<branchname> to set up upstream tracking, or viewing available branches first via git branch. Examples: - git checkout main - git checkout feature/new-ui

Examples:

  • git checkout main
Related commands

Welcome to GitExamples!

Sign in to enable bookmarking, reminders, progress-tracking and more...