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

hg clone -r 3 /path/to/repository

当前回答

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

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

Git获取原点2600f4f928773d79164964137d514b85400b09b2

git checkout FETCH_HEAD

其他回答

对于单个文件,当提交号已知时,可以使用wget onliner:

wget https://raw.githubusercontent.com/torvalds/linux/896066ee1cf4d653057dac4e952f49c96ad16fa7/README

总结一下(git v. 1.7.2.1):

在你想要回购的地方做一个常规的git克隆(得到所有的日期-我知道,不是想要的,我们正在那里) Git checkout <sha1 rev>你想要的rev Git重置——很难 Git checkout -b master

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上传到它的新家。

希望这能帮助到别人

我使用这个代码片段和GNU make来关闭任何修订标记、分支或散列

这是在 Git 2.17.1版本

${dir}:
    mkdir -p ${@D}
    git clone --recursive --depth 1 --branch ${revison} ${url} ${@} \
 || git clone --recursive --branch ${revison} ${url} ${@} \
 || git clone ${url} ${@}
    cd ${@} && git reset --hard ${revison}
    ls $@




从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