1

I have a git repository and I want to just download the state of that repository at a particular commit. How do I do that?

I don't intend to make any changes to the repository afterwards, and I don't need the .git folder as I do not want to download unnecessary files. I just want the code in a particular commit.

Is there a way to do that with git?

xiaodai
  • 14,889
  • 18
  • 76
  • 140
  • Why not just download the entire repository, check out the specific commit, and then delete the `.git` folder? – Obsidian Age Sep 13 '19 at 02:43
  • Takes longer! And saving bandwidth. Consider ppl on Raspberry pi's – xiaodai Sep 13 '19 at 03:11
  • Possible duplicate of [How to clone git repository with specific revision/changeset?](https://stackoverflow.com/questions/3489173/how-to-clone-git-repository-with-specific-revision-changeset) – Obsidian Age Sep 13 '19 at 03:52
  • Not a dup! This is about not cloning the whole repo! – xiaodai Sep 13 '19 at 04:23
  • Possible duplicate of [Git: get specific revision from remote server](https://stackoverflow.com/questions/25472155/git-get-specific-revision-from-remote-server) – Gino Mempin Sep 13 '19 at 06:23
  • The closest you can get to doing this is to use [`git archive`](https://git-scm.com/docs/git-archive) with the [`--remote`](https://git-scm.com/docs/git-archive#Documentation/git-archive.txt---remoteltrepogt) option, but as mentioned in the docs, it relies on specific repo configs to get it properly working with a plain sha1 of the commit. A workaround, would be to tag the commit, then depending on the server (Github, Gitlab), download a copy of the repo as a tar.gz using the tag. – Gino Mempin Sep 13 '19 at 06:29
  • Possible duplicate of [How to shallow clone a specific commit with depth 1?](https://stackoverflow.com/questions/31278902/how-to-shallow-clone-a-specific-commit-with-depth-1) – phd Sep 13 '19 at 11:05
  • https://stackoverflow.com/search?q=%5Bgit%5D+shallow+clone+specific+commit – phd Sep 13 '19 at 11:05

2 Answers2

1

You can do it with recent enough git and it should be enabled on a server.

Here is how to fetch and checkout commit 71bbabb16755c3d611bb24909e77a0df688827aa as local branch 'main' from https://github.com/curl/curl:

git init curl
cd curl
git fetch --depth=1 https://github.com/curl/curl 71bbabb16755c3d611bb24909e77a0df688827aa:refs/heads/main
git checkout main
redbaron
  • 398
  • 3
  • 11
0

You can't just download a particular commit. Instead of you can clone a specific branch from the remote repository and then switch to any commit.

Here is the syntax of the command to clone the specific git branch.

git clone -b <BRANCH_NAME> <GIT_REMOTE_URL>

You can use --single-branch to prevent fetching details of other branches like below:

git clone -b <BRANCH_NAME> --single-branch <GIT_REMOTE_URL>

If you go to the directory of your downloaded project and run the following command:

git branch -a

you can see the .git folder does not include information about other branches.

So now, fetch your desired commit by the following command:

git checkout <COMMIT_SHA1_KEY>

Note: You can clone only the latest commit from a given branch by using --depth=n

git clone -b <BRANCH_NAME> --single-branch --depth=2 <GIT_REMOTE_URL>
Fatema Tuz Zuhora
  • 3,088
  • 1
  • 21
  • 33