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.
Resolve conflicts and master branch mechanics
Understand 3-way diffs, conflict marker anatomy, and rebasing playbooks in the 97-page offline course — ₹499.
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
- Guide
Git Branching Strategies: Git Flow vs Trunk-Based Development
Compare the most popular branching models and learn when to use merge, rebase, and squash for a clean project history.
8 min readRead guide - Guide
Git Rebase vs Merge: When to Use Each
A clear breakdown of the trade-offs between rebasing and merging, with examples of when each approach makes sense.
5 min readRead guide - Guide
Resolving Git Merge Conflicts: A Practical Guide
Conflicts are inevitable. Learn how to read conflict markers, resolve them systematically, and use tools to make merging painless.
6 min readRead guide
Related commands
More from Branching & Merging