git,

Checkout a specific Git commit

Oct 02, 2022 · 1 min read · Post a comment

Checking out a Git commit is a common practice mostly used for troubleshooting purposes.

Prerequisites

  • Git

Solution

Step 1. Clone the repository.

git clone https://github.com/<username_or_org>/<repo_name>.git
cd <repo_name>

Step 2. List Git commits with:

git log

Step 3. Find the commit you want to checkout from, copy the commit ID (SHA1 hash) and run:

git checkout <commit-SHA1>

Note(s): Once you checked out, the HEAD will point to the commit ID hence leading to the so-called detached HEAD mode.

Step 4. If you want to undo this operation and not save any work done, use:

git switch -

or, checkout another branch. For instance:

git checkout main

Conclusion

To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.

git