Last Updated: December 29, 2022
·
478.3K
· ihcsim

Git - Cloning Specific Commits

For an answer on why there isn't commit-specific clone in git, please refer to my original question on stackoverflow.

As you probably know, the concept of cloning in git is different from that of a svn checkout. With svn, I can checkout a working copy of a specific revision of the repository using the -r option. In git, cloning a repository involves, well, cloning the entire repository, and setting its HEAD to the latest commit (unless the -n option is specified).

Recently, I come across two use cases where I need specific commits from repositories on github which I don't own.

  1. Case 1: I need to work with a specific commit of a submodule in a repo.
  2. Case 2: I need to include reference to a commit of a depended repo in my documentation.

Here are some ways to get a specific commit of a repo.

  • Github Tree View

If the repo is in github, you can navigate to the tree view of the repo at https://github.com/<repo_name>/tree/<commit_sha>

Then clicking on the Download ZIP button on the right-hand navigation bar will download the codes of that repo up to the specified commit.

  • Clone The Repo And Checkout The Specific Commit

This will set the HEAD of your master to point to commit_sha.

git clone -n <repo_name> 
git checkout <commit_sha>
  • Clone The Repo And Checkout The Specific Commit Into A Branch

This will set the HEAD of your new_branch to point to commit_sha.

git clone -n <repo_name>
git checkout -b <new_branch> <commit_sha>

If you are aware of other ways of doing this, please share. Thanks!

3 Responses
Add your response

Thanks

over 1 year ago ·

thanks...thiss really helped

over 1 year ago ·

okay, thx!

over 1 year ago ·