Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone a repository from a specific commit ID?

I'm trying to clone using HTTPs a repository (sample.com/component.git) but from a specific commit ID.

Should I fetch the repoistory, then checkout that commit ID and finally clone it, or what would be a better way?

git fetch sample.com/component.git
git checkout 9a36b9e79tbb9132c7020a5bd5694c7cefce8cff
git clone sample.com/component.git

Or

git clone sample.com/component.git 9a36b9e79tbb9132c7020a5bd5694c7cefce8cff
like image 493
tadm123 Avatar asked Nov 16 '25 03:11

tadm123


2 Answers

Try this 3 steps,

Step 1: clone the main repo:

git clone sample.com/component.git

Get inside the cloned repo cd folder_name. Now you have a working directory with latest version pulled and you want to move back to a specific commit id, here's how you can do it:

Step 2:

git reset --hard 9a36b9e79tbb9132c7 #(takes you back to that commit)

Step 3:

git clean -df #(cleans any untracked files/folders)
like image 195
Ishteaque Alam Avatar answered Nov 18 '25 19:11

Ishteaque Alam


Git 2.49, released in 2025-03-14 added support for this with the “--revision” in the clone command.

https://git-scm.com/docs/git-clone#Documentation/git-clone.txt-code--revisionltrevgtcode

git clone --revision=683c54c999c301c2cd6f715c411407c413b1d84e --depth=1 https://github.com/gig/git.git
like image 31
arslivinski Avatar answered Nov 18 '25 20:11

arslivinski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!