How to checkout a commit in Git

Checking out specific commits allows you to examine code at particular points in history, useful for debugging, code review, and understanding changes in your Git repository. As the creator of CoreUI, a widely used open-source UI library, I’ve performed countless commit checkouts for debugging issues and reviewing historical changes across multiple repositories. From my 25 years of experience in software development and version control, the most straightforward approach is to use git checkout with the commit hash. This method provides safe examination of historical code states without affecting your current work.

Use git checkout with the commit hash to examine specific commits in your repository history.

# Checkout a specific commit using full hash
git checkout a1b2c3d4e5f6789012345678901234567890abcd

# Checkout using shortened hash (first 7-8 characters)
git checkout a1b2c3d

# Checkout a commit relative to HEAD
git checkout HEAD~3

# View commit history to find the hash
git log --oneline

# Return to the latest commit on current branch
git checkout main

The git checkout command with a commit hash puts your repository in “detached HEAD” state, allowing you to examine files and run tests without affecting any branch. You can use the full 40-character hash or a shortened version (typically 7-8 characters). Relative notation like HEAD~3 checks out the commit 3 steps before the current HEAD. Git will warn you about detached HEAD state and suggest creating a branch if you want to make changes.

This is the same commit checkout workflow we use in CoreUI development for investigating historical bugs and understanding code evolution. Always return to a proper branch with git checkout branch-name when you’re done examining historical commits to avoid accidentally losing work in detached HEAD state.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Conditionally Add a Property to an Object in JavaScript
How to Conditionally Add a Property to an Object in JavaScript

How to loop inside React JSX
How to loop inside React JSX

What Does javascript:void(0) Mean?
What Does javascript:void(0) Mean?

How to Open All Links in New Tab Using JavaScript
How to Open All Links in New Tab Using JavaScript

Answers by CoreUI Core Team