US Trends

what does git checkout do

Git checkout is the command that lets you move around your project’s history and branches, and optionally restore files to a specific version. It changes three things at once: your current branch (or commit), the files in your working directory, and what HEAD points to.

What does git checkout do?

In plain terms:

git checkout = “Go to this branch/commit, and update my files to match it.”

Core behaviors:

  • Switch branches : git checkout my-branch moves you from your current branch (say main) to my-branch, updating all tracked files to match that branch’s latest commit.
  • Create + switch to a new branch : git checkout -b feature/payment creates a new branch named feature/payment from the current commit and immediately switches to it.
  • Jump to a specific commit (detached HEAD) : git checkout <commit-hash> moves HEAD to that commit without being on a branch (a “detached HEAD”), so you’re viewing an old snapshot.
  • Restore file versions : git checkout <branch-or-commit> -- path/to/file replaces that file in your working directory with the version from that branch/commit.

Mini sections: the main use cases

1. Switching branches

When you run:

bash

git checkout main
git checkout feature/login

Git will:

  • Point HEAD to the chosen branch.
  • Update your working directory files to match that branch’s latest commit.

You’ll usually see:

bash

Switched to branch 'feature/login'

This is how you “move” between lines of development in a repo.

2. Creating a new branch and moving there

Command:

bash

git checkout -b feature/payment

This is a shorthand for:

bash

git branch feature/payment
git checkout feature/payment

It:

  • Creates a new branch starting at the current commit.
  • Immediately switches you to it.

Developers use this pattern constantly for new features or bugfixes.

3. Detached HEAD: checking out a commit

Command:

bash

git checkout 1a2b3c4d

This moves HEAD directly to that commit instead of to a branch name.

Effects:

  • You see the project exactly as it was at that commit.
  • You are not on a named branch (you’re “detached”).
  • New commits from here will not belong to any branch unless you create one (e.g. git checkout -b hotfix-from-old-commit).

This is great for debugging, bisecting, or quickly inspecting older states.

4. Restoring specific files

If you just want to fix one file:

bash

git checkout main -- path/to/file.txt

This will:

  • Take file.txt as it exists on main.
  • Overwrite your current file.txt in the working directory.

You can also restore from a commit:

bash

git checkout 1a2b3c4d -- src/app.js

This is like saying, “Give me the version of src/app.js from that old commit.”

A quick mental model (story-ish)

Imagine your repository as a timeline with many roads (branches).

  • Branches are roads with names like main, dev, feature/login.
  • Commits are checkpoints along each road.
  • HEAD is the marker saying “You are here”.

When you run git checkout feature/login, you drive onto the feature/login road and park at its latest checkpoint. Your working directory is the scenery around your car—Git updates it to match exactly what’s at that checkpoint.

When you run git checkout <commit>, you drive to a specific checkpoint somewhere on some road, but you’re no longer following that road’s future—just standing at that point in history (detached HEAD).

Common patterns and tips

  • Start a new feature:

    bash
    
    git checkout -b feature/amazing-idea
    
  • Jump back to main:

    bash
    
    git checkout main
    
  • Quickly jump to previous branch:

    bash
    
    git checkout -
    
  • Restore a broken file from main:

    bash
    
    git checkout main -- package.json
    
  • Explore an old commit safely:

    bash
    
    git checkout <old-commit>
    # look around, run tests, etc.
    git checkout main
    

All of these revolve around the same idea: change whereHEAD is and make your files match that place.

HTML table of key git checkout usages

html

<table>
  <thead>
    <tr>
      <th>Command</th>
      <th>What it does</th>
      <th>Typical use</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>git checkout &lt;branch&gt;</code></td>
      <td>Switches to the given branch and updates working files to match its latest commit.[web:3][web:5]</td>
      <td>Move between <code>main</code>, feature branches, bugfix branches, etc.[web:5]</td>
    </tr>
    <tr>
      <td><code>git checkout -b &lt;new-branch&gt;</code></td>
      <td>Creates a new branch from current commit and switches to it in one step.[web:1][web:3][web:5]</td>
      <td>Starting a new feature or experiment from your current state.[web:5]</td>
    </tr>
    <tr>
      <td><code>git checkout &lt;commit&gt;</code></td>
      <td>Moves <code>HEAD</code> to a specific commit (detached HEAD), updating files to that snapshot.[web:3][web:9]</td>
      <td>Inspecting or debugging a past revision without changing branches.[web:9]</td>
    </tr>
    <tr>
      <td><code>git checkout &lt;branch-or-commit&gt; -- &lt;path&gt;</code></td>
      <td>Restores one or more files from a branch or commit into your working directory.[web:3]</td>
      <td>Undoing changes to a file by taking the version from <code>main</code> or an older commit.</td>
    </tr>
    <tr>
      <td><code>git checkout -</code></td>
      <td>Switches back to the previous branch you were on.[web:3]</td>
      <td>Toggling quickly between two branches while working.</td>
    </tr>
  </tbody>
</table>

TL;DR

  • git checkout moves you to a branch or commit and updates your files to match it.
  • It’s used to switch branches , create and switch to new branches , explore old commits , and restore file versions.

Information gathered from public forums or data available on the internet and portrayed here.