我如何用特定的版本克隆git存储库,就像我通常在Mercurial中做的那样:

hg clone -r 3 /path/to/repository

当前回答

克隆一个git存储库,恰当地说,就是克隆整个存储库:没有办法只选择一个修订版本来克隆。然而,一旦你执行了git克隆,你可以通过执行checkout <rev>来检出一个特定的修订。

其他回答

克隆一个git存储库,恰当地说,就是克隆整个存储库:没有办法只选择一个修订版本来克隆。然而,一旦你执行了git克隆,你可以通过执行checkout <rev>来检出一个特定的修订。

从Git 2.5.0开始,下面描述的功能可以在服务器端通过配置变量uploadpack启用。allowReachableSHA1InWant,这里是GitHub特性请求和GitHub提交启用该特性。注意,一些Git服务器默认激活这个选项,例如Bitbucket Server从5.5+版本开始启用它。有关如何激活配置选项的示例,请参阅Stackexchange上的回答。

更新1对于Git版本1.7 < v < 2.5使用Git克隆和Git重置,如Vaibhav Bajpai的回答所述

如果你不想获取完整的存储库,那么你可能不应该使用clone。你可以使用fetch来选择你想要获取的分支。我不是hg专家,所以我不知道-r的细节,但在git中你可以做这样的事情。

# make a new blank repository in the current directory
git init

# add a remote
git remote add origin url://to/source/repository

# fetch a commit (or branch or tag) of interest
# Note: the full history up to this commit will be retrieved unless 
#       you limit it with '--depth=...' or '--shallow-since=...'
git fetch origin <sha1-of-commit-of-interest>

# reset this repository's master branch to the commit of interest
git reset --hard FETCH_HEAD

Using 2 of the above answers (How to clone git repository with specific revision/changeset? and How to clone git repository with specific revision/changeset?) Helped me to come up with a definative. If you want to clone up to a point, then that point has to be a tag/branch not simply an SHA or the FETCH_HEAD gets confused. Following the git fetch set, if you use a branch or tag name, you get a response, if you simply use an SHA-1 you get not response. Here's what I did:- create a full working clone of the full repo, from the actual origin

cd <path to create repo>
git clone git@<our gitlab server>:ui-developers/ui.git 

然后在有趣的地方创建一个本地分支

git checkout 2050c8829c67f04b0db81e6247bb589c950afb14
git checkout -b origin_point

然后创建我的新的空白回购,我的本地副本作为它的起源

cd <path to create repo>
mkdir reduced-repo
cd reduced-repo
git init
git remote add local_copy <path to create repo>/ui
git fetch local_copy origin_point

那时我得到了这样的回应。我指出这一点是因为如果使用SHA-1来代替上面的分支,什么也不会发生,所以响应意味着它工作了

/var/www/html/ui-hacking$ git fetch local_copy origin_point
remote: Counting objects: 45493, done.
remote: Compressing objects: 100% (15928/15928), done.
remote: Total 45493 (delta 27508), reused 45387 (delta 27463)
Receiving objects: 100% (45493/45493), 53.64 MiB | 50.59 MiB/s, done.
Resolving deltas: 100% (27508/27508), done.
From /var/www/html/ui
 * branch            origin_point -> FETCH_HEAD
 * [new branch]      origin_point -> origin/origin_point

现在在我的情况下,我需要把它放回gitlab,作为一个新的repo,所以我做了

git remote add origin git@<our gitlab server>:ui-developers/new-ui.git

这意味着我可以通过使用git——git-dir=../ui/从origin_point重建我的repo。Git format-patch -k -1——stdout <sha1> | Git am -3 -k来远程拾取樱桃,然后使用Git push origin将整个lot上传到它的新家。

希望这能帮助到别人

我可以使用git clone——config选项来完成这一点,这是我从下面的回答中学到的: https://stackoverflow.com/a/43759576/1330650

我的场景涉及Azure DevOps管道中的稀疏签出,其中我需要使用提交散列(而不是分支名称)克隆一个回购。clone命令不接受提交哈希作为参数。解决方法是设置一个包含refspec的配置变量(-c),因为该refspec可以使用提交哈希而不是分支名称:

git clone -c remote.origin.fetch=+<commit hash>:refs/remotes/origin/<commit hash> <repo_url> --no-checkout --progress --depth 1
git sparse-checkout init --cone
git sparse-checkout set <file list>
git checkout <commit hash>

git克隆https://github.com/ORGANIZATION/repository.git(克隆存储库)

CD存储库(导航到存储库)

Git获取原点2600f4f928773d79164964137d514b85400b09b2

git checkout FETCH_HEAD