git
version control
repository
commit
tutorial

How can I switch my git repository to a particular commit

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To switch a Git repository to a particular commit, you usually check out that commit directly or switch to it in detached HEAD mode. The important detail is that viewing an old commit and moving a branch pointer are different operations, and Git gives you different commands depending on which one you actually want.

View a Specific Commit Without Moving a Branch

If you simply want to inspect or test the repository at an older commit, use detached HEAD mode.

bash
git switch --detach <commit-hash>

Older Git syntax that does the same thing is:

bash
git checkout <commit-hash>

In detached HEAD mode, HEAD points directly to the commit rather than to a branch name.

What Detached HEAD Means

Detached HEAD is not an error. It just means you are no longer on a branch tip.

That is useful for:

  • inspecting old code,
  • testing a historical state,
  • bisecting bugs,
  • building from a specific commit.

You can read files, run tests, and even make commits there. The only catch is that new commits will not belong to a named branch unless you create one.

Create a Branch If You Want to Keep Working

If you switch to a commit and then decide you want to keep changes based on that state, create a branch.

bash
git switch --detach <commit-hash>
git switch -c my-fix-branch

That turns your temporary historical checkout into a normal branch workflow again.

Move the Current Branch Pointer Intentionally

Sometimes "switch my repository to a commit" really means "move my current branch back to that commit." That is a different operation, and it is usually done with reset.

bash
git reset --hard <commit-hash>

This rewrites the current branch tip and discards uncommitted working-tree changes. It is much more destructive than a detached checkout, so use it only when you truly mean to move branch history locally.

If you want to keep the working-tree changes while moving the branch pointer, use a softer reset mode:

bash
git reset --mixed <commit-hash>

Choose reset modes carefully. They are not just navigation commands.

How to Get Back

If you only checked out a commit for inspection, returning is easy:

bash
git switch main

Or switch back to whichever branch you were on:

bash
git switch -

That is one reason detached HEAD is a safe default for exploration. It lets you inspect history without rewriting anything first. You can always branch from that state later if the inspection turns into real work. That flexibility is useful.

Common Pitfalls

  • Using reset --hard when a detached checkout would have been safer.
  • Forgetting that commits created in detached HEAD mode are not attached to a branch unless you create one.
  • Switching commits with uncommitted local changes and then being surprised by conflicts or blocked checkout.
  • Confusing "view this commit" with "move my branch to this commit."
  • Using old checkout syntax without understanding whether you are switching branches or checking out a commit directly.

Summary

  • Use git switch --detach <commit> to inspect a specific commit safely.
  • Detached HEAD is normal when you are viewing history directly.
  • Create a branch if you want to keep working from that commit.
  • Use reset only when you intentionally want to move a branch pointer.
  • Prefer exploration with detached HEAD over destructive history moves.

Course illustration
Course illustration

All Rights Reserved.