简体   繁体   English

Git:如何只下载一个特定的提交而不克隆存储库的其余部分?

[英]Git: How do download just a particular commit and not clone the rest of the repository?

I have a git repository and I want to just download the state of that repository at a particular commit.我有一个 git 存储库,我只想在特定提交时下载该存储库的状态。 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.之后我不打算对存储库进行任何更改,也不需要 .git 文件夹,因为我不想下载不必要的文件。 I just want the code in a particular commit.我只想要特定提交中的代码。

Is there a way to do that with git?有没有办法用 git 做到这一点?

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 分支的命令语法。

git clone -b <BRANCH_NAME> <GIT_REMOTE_URL>

You can use --single-branch to prevent fetching details of other branches like below:您可以使用--single-branch来防止获取其他分支的详细信息,如下所示:

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.您可以看到.git文件夹不包含有关其他分支的信息。

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注意:您可以使用--depth=n仅克隆给定分支的最新提交

git clone -b <BRANCH_NAME> --single-branch --depth=2 <GIT_REMOTE_URL>

You can do it with recent enough git and it should be enabled on a server.您可以使用最新的 git 来执行此操作,并且应该在服务器上启用它。

Here is how to fetch and checkout commit 71bbabb16755c3d611bb24909e77a0df688827aa as local branch 'main' from https://github.com/curl/curl :这是从https://github.com/curl/curl获取和签出提交71bbabb16755c3d611bb24909e77a0df688827aa作为本地分支“主”的方法:

git init curl
cd curl
git fetch --depth=1 https://github.com/curl/curl 71bbabb16755c3d611bb24909e77a0df688827aa:refs/heads/main
git checkout main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM