How to Checkout a Specific Commit in Git

Git's ability to navigate through the repository's history by checking out specific commits is super useful for for reviewing code, debugging, and undoing changes. This post covers how do to that.

How to identify the commit hash in Git?

To start, find the Git commit hash you need by using the git log command. Adding --oneline simplifies the output, making it easier to locate the specific commit.

git log --oneline

Look for the commit hash, which is a 40-character string, associated with the commit you're interested in.

How to checkout the commit in Git?

With the commit hash in hand, proceed to checkout the commit by using the git checkout command followed by the hash.

git checkout a1b2c3d

Ensure you replace a1b2c3d with your specific commit hash. This action will put you in a 'detached HEAD' state, which is normal and allows for temporary exploration and changes.

How to work with the detached HEAD?

If you wish to make changes while in this state, and want to keep them, it's wise to start a new branch from here.

git checkout -b new-branch-name

This command branches out from your current commit, ensuring any new changes are saved and organized.

How to return to a branch in Git?

When ready to go back to your main work, switch back to your desired branch as follows.

git checkout main

Replace main with whichever branch you wish to return to.

Navigating Git's history by checking out specific commits is a fundamental skill that bolsters your ability to manage your projects effectively. Always remember to branch off from a detached HEAD state to keep your changes organized and accessible.

The next generation of charts and BI.

Coming soon.

Fast. Opinionated. Collaborative. Local-first. Keyboard centric.
Crafted to the last pixel. We're looking for early alpha users.