Back
GitoryxGitoryx
Beginner9 min read

Git Checkout: Switch Branches, Files, and Commits

git 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.

The Problem

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:

  • Ending up in a detached HEAD state and making commits that get lost
  • Using `git checkout <file>` to discard changes and permanently losing work
  • Forgetting to create a branch after checking out a specific commit
  • Confusion between `git checkout branch` and `git checkout -- file`

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.

What is Checkout: Switch Branches, Files, and Commits?

`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.

Step-by-Step Guide

1

Switch to an existing branch

The most common use. Both syntaxes work; `git switch` is clearer.

bash
# Modern (recommended)
git switch feature/login

# Classic
git checkout feature/login
2

Create and switch to a new branch

Create a branch and check it out in one step.

bash
# Modern
git switch -c feature/new-thing

# Classic
git checkout -b feature/new-thing
3

Check out a specific commit (detached HEAD)

You can navigate to any commit by its SHA. Git enters 'detached HEAD' mode — you're not on any branch.

bash
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`.

4

Check out a remote branch locally

To start working on a branch that exists only on the remote, create a local tracking branch.

bash
git switch -c feature/remote-work origin/feature/remote-work
# or shorthand:
git checkout --track origin/feature/remote-work
See this workflow in Gitoryx — Gitoryx demo

See this workflow in Gitoryx. Double-click any branch or commit in the graph to check it out

Free download
5

Restore a single file to its last committed state

Discard all changes to a specific file and revert it to the last commit.

bash
# Modern (recommended — no ambiguity)
git restore src/app.ts

# Classic (still works but ambiguous syntax)
git checkout -- src/app.ts

This permanently discards your uncommitted changes to that file. There is no undo.

6

Restore a file from a specific commit

Pull a specific version of a file from any commit into your working directory.

bash
git restore --source a3f2c1d src/app.ts
# or classic:
git checkout a3f2c1d -- src/app.ts

This doesn't change your branch — it just replaces the file content with the version from that commit.

Common Mistakes to Avoid

Making commits in detached HEAD state

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.

Using `git checkout -- <file>` accidentally

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.

Not knowing the difference between `git switch` and `git checkout`

`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.

Gitoryx — visual Git client for macOS, Windows & Linux
GitoryxGitoryx — macOS, Windows & Linux

Navigate commits and branches visually in Gitoryx

  • Double-click any branch or commit in the graph to check it out
  • Detached HEAD state is clearly indicated with a special icon
  • Create a branch from a detached HEAD in one click
  • Restore individual files with right-click → Discard changes
Download Gitoryx — FreemacOS · Windows · Linux · No subscription

Frequently Asked Questions

What is detached HEAD in Git?

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.

What is the difference between `git checkout` and `git switch`?

`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.

How do I get out of detached HEAD state?

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.

Can I check out a file without switching branches?

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.

See it in action with Gitoryx

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.