Skip to content

Instantly share code, notes, and snippets.

@jcdiv47
Created August 21, 2024 06:34
Show Gist options
  • Save jcdiv47/4a26353972c8b610a3b4358f9f61add1 to your computer and use it in GitHub Desktop.
Save jcdiv47/4a26353972c8b610a3b4358f9f61add1 to your computer and use it in GitHub Desktop.
Git Checkout Certain Commit
  1. To visit the state of a repository under certain commit, first clone the repo without checkout:
git clone https://github.com/karpathy/build-nanogpt.git --no-checkout

This command downloads all the repository data, but does not check out the working files.

  1. Navigate to the directoryone should see an seemingly empty directory.
cd build-nanogpt
  1. Checkout with a specific commit hash:
git checkout <commit-hash>

Tip

This command updates your working directory to match the state of the repository at the specified commit. Keep in mind that when you check out a commit directly, you might enter a "detached HEAD" state, meaning you are not on a branch. To return to a branch, you can use:

git checkout <branch-name>

This will point your working directory to the latest commit on that branch.

How do I obtain the full commit hash?

If you have the partial commit hash such as 28916d9, use command git show 28916d9 to display the full commit hash along with the commit's details, such as the author, date, and the changes made. Finally, command git rev-parse 28916d9 will give the full hash.

You can use this one-line command:

git checkout $(git rev-parse 28916d9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment