Switch to an existing branch
The most common use. Both syntaxes work; `git switch` is clearer.
# Modern (recommended)
git switch feature/login
# Classic
git checkout feature/logingit checkout is one of Git's most versatile commands. This guide covers switching branches, checking out specific commits (detached HEAD), restoring individual files, and how the newer git switch and git restore commands relate to it.
You run `git checkout` to switch branches and it works — but then you use it to look at an old commit and Git tells you you're in a 'detached HEAD state'. What does that even mean?
`git checkout` is one of Git's oldest and most overloaded commands. It can switch branches, restore files, and navigate to specific commits. Understanding what it does in each case — and when to use the newer `git switch` and `git restore` instead — will save you from confusing mistakes.
Common mistakes developers make with this:
Gitoryx: In Gitoryx, double-clicking a branch or commit in the graph checks it out instantly — with a clear visual indicator when you're in detached HEAD state.
`git checkout` switches branches, restores files, or navigates to a specific commit. Modern Git (2.23+) splits this into two focused commands: `git switch` for branches and `git restore` for files.
The most common use. Both syntaxes work; `git switch` is clearer.
# Modern (recommended)
git switch feature/login
# Classic
git checkout feature/loginCreate a branch and check it out in one step.
# Modern
git switch -c feature/new-thing
# Classic
git checkout -b feature/new-thingYou can navigate to any commit by its SHA. Git enters 'detached HEAD' mode — you're not on any branch.
git checkout a3f2c1d
# Git warns you:
# HEAD is now at a3f2c1d add login form
# You are in 'detached HEAD' state.In detached HEAD state, any commits you make won't belong to a branch. If you switch away, those commits become unreachable. Create a branch immediately if you want to keep them: `git switch -c my-branch`.
To start working on a branch that exists only on the remote, create a local tracking branch.
git switch -c feature/remote-work origin/feature/remote-work
# or shorthand:
git checkout --track origin/feature/remote-work
See this workflow in Gitoryx. Double-click any branch or commit in the graph to check it out
Free downloadDiscard all changes to a specific file and revert it to the last commit.
# Modern (recommended — no ambiguity)
git restore src/app.ts
# Classic (still works but ambiguous syntax)
git checkout -- src/app.tsThis permanently discards your uncommitted changes to that file. There is no undo.
Pull a specific version of a file from any commit into your working directory.
git restore --source a3f2c1d src/app.ts
# or classic:
git checkout a3f2c1d -- src/app.tsThis doesn't change your branch — it just replaces the file content with the version from that commit.
Commits made in detached HEAD are not reachable by any branch. When you switch branches, Git warns you they'll be lost.
Fix: As soon as you realize you're in detached HEAD and want to keep your work, run `git switch -c <new-branch>` to attach your commits to a real branch.
It looks similar to other checkout commands but discards all your uncommitted file changes permanently.
Fix: Prefer `git restore <file>` — it's explicit about restoring files and harder to mistype. Always run `git status` before restoring to confirm you're targeting the right file.
`git checkout` does too many things; `git switch` and `git restore` were introduced in Git 2.23 to make the intent clear.
Fix: Use `git switch` for branch operations and `git restore` for file operations going forward.

Detached HEAD means your HEAD pointer points directly to a commit instead of a branch. You can browse and even commit, but those commits won't belong to any branch and will be lost if you check out another branch without creating a new one first.
`git switch` was introduced in Git 2.23 as a focused replacement for the branch-switching part of `git checkout`. Similarly, `git restore` handles file restoration. The old `git checkout` still works but does too many different things.
Run `git switch <branch-name>` to return to a branch. If you made commits you want to keep, first run `git switch -c <new-branch>` to save them.
Yes. `git restore <file>` restores a file to its last committed state without changing your current branch. Use `git restore --source <sha> <file>` to restore from a specific commit.
Everything in this tutorial is faster and clearer with a visual Git client. Gitoryx is free, runs natively on macOS, Windows, and Linux, and built for developers who want to move fast without breaking things.