1

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

I really don't know where to start. Should I fetch the repoistory, then checkout that commit ID and finally clone it?

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

OR

git clone sample.com/component.git 9a36b9e79tbb9132c7020a5bd5694c7cefce8cff

Any help in doing this correctly, or if there's a better way would be highly appreciated

tadm123
  • 8,294
  • 7
  • 28
  • 44
  • 2
    Does this help? https://stackoverflow.com/questions/26135216/why-isnt-there-a-git-clone-specific-commit-option – evolutionxbox Feb 15 '23 at 02:21
  • Git does not have a concept of a clone of a particular commit. Cloning is a function on (whole) repositories. Having cloned a repository, you can check out any commit you like into your clone's working directory. – John Bollinger Feb 15 '23 at 02:28
  • @JohnBollinger - is that true? I think the existence of the -single-branch branch option to the commit command says otherwise. I don't know, however, if this what the OP is looking for. – CryptoFool Feb 15 '23 at 02:34
  • @tadm123 - can you provide a bit more information about what you're trying to accomplish? Do you want a repository that contains all of the history for a particular commit, or do you just want the files represented by a particular commit? – CryptoFool Feb 15 '23 at 02:35
  • @CryptoFool I'll be honest this is a new job in software engineering which I have little experience, I have cloned a repository that has a components folder in it. He wants me to add into this component folder another submodule. I feel completely lost. This is the exact task: "add this special component as a submodule to compoennt folder as well sample.com/component.git 9a36b9e79tbb9132c7020a5bd5694c7cefce8cff" – tadm123 Feb 15 '23 at 02:43
  • Apparently that url has a folder that he wants me added, but I'm not sure how exactly the commit ID comes into play here. I think he wants me to download that commit ID version of the repository... – tadm123 Feb 15 '23 at 02:45
  • @CryptoFool, I guess you mean `git clone --single-branch` (not `git commit -single-branch`). But that's still a repository operation, and it yields a repository containing the full history of the tip of the specified branch. It is not a clone of a single commit. – John Bollinger Feb 15 '23 at 02:50
  • oops...yeah, that's what I meant. And as far as "a clone of a single commit" goes, that's exactly what I'm asking the OP to clarify...what do they mean by "clone a single commit". I think you can actually clone a repo to contain just a single commit with `git clone --single-branch --branch --depth 1`. – CryptoFool Feb 15 '23 at 02:53
  • In any case, if the objective is to set up a submodule, then I'm pretty sure that cloning is a red herring. – John Bollinger Feb 15 '23 at 02:54
  • @JohnBollinger "cloning is a red herring". agreed. – CryptoFool Feb 15 '23 at 02:56
  • @John Bollinger Can you let me know if the task not about cloning, what exactly are they asking me to do? I'm a little confused, what does "setting up a submodule" mean? – tadm123 Feb 15 '23 at 03:19
  • @CryptoFool can you guys please let me know what is he reffering to when he says "setting up a submodule" if it's not about cloning? – tadm123 Feb 15 '23 at 03:37
  • @tadm123, a "submodule" is a specific structure within a Git repository. Look up [`git submodule`](https://git-scm.com/docs/git-submodule). – John Bollinger Feb 15 '23 at 13:16
  • Does this answer your question? [Git clone particular version of remote repository](https://stackoverflow.com/questions/3555107/git-clone-particular-version-of-remote-repository) – Nol4635 Aug 04 '23 at 23:24

1 Answers1

1

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)