如何克隆具有特定修订/变更集的git存储库?

半城伤御伤魂 提交于 2020-02-25 19:17:08

如何克隆具有特定修订版的git存储库,就像我通常在Mercurial中所做的那样:

hg clone -r 3 /path/to/repository

#1楼

$ git clone $URL
$ cd $PROJECT_NAME
$ git reset --hard $SHA1

再次回到最近的提交

$ git pull

#2楼

您可以简单地使用git checkout <commit hash>

按此顺序

bash git clone [URLTORepository] git checkout [commithash]

提交哈希看起来像这样“ 45ef55ac20ce2389c9180658fdba35f4a663d204”


#3楼

TL; DR-只需针对要克隆的提交在源存储库中创建一个标签,然后在fetch命令中使用该标签。 您可以稍后从原始存储库中删除标签以进行清理。

好吧,它的2014年,以及查尔斯·贝利(Charles Bailey)从2010年开始接受的答案,现在看来已经完全过时了,其他大多数(所有?)答案都涉及克隆,这是许多人希望避免的。

以下解决方案实现了OP及其它许多人正在寻找的解决方案,这是一种创建存储库副本的方法,包括历史记录,但仅限于一定的提交。

以下是我在git版本2.1.2中使用的命令,用于将本地存储库(即另一个目录中的存储库)克隆到特定点:

# in the source repository, create a tag against the commit you want to check out
git tag -m "Temporary tag" tmptag <sha1>

# create a new directory and change into that directory
cd somewhere_else;mkdir newdir;cd newdir

# ...and create a new repository
git init

# add the source repository as a remote (this can be a URL or a directory)
git remote add origin /path/to/original/repo

# fetch the tag, which will include the entire repo and history up to that point
git fetch origin refs/tags/tmptag

# reset the head of the repository
git reset --hard FETCH_HEAD

# you can now change back to the original repository and remove the temporary tag
cd original_repo
git tag -d tmptag

希望该解决方案可以继续工作几年! :-)


#4楼

git clone -o <sha1-of-the-commit> <repository-url> <local-dir-name>

git使用单词origin代替流行的revision

以下是手动$ git help clone的摘录

--origin <name>, -o <name>
    Instead of using the remote name origin to keep track of the upstream repository, use <name>.

#5楼

使用上述答案中的2个( 如何使用特定的修订/更改集 克隆git存储库 以及如何使用特定的修订/更改集克隆git存储库? )帮助我提出了一个明确的定义。 如果要克隆到某个点,则该点必须是标记/分支,而不仅仅是SHA或FETCH_HEAD会混淆。 遵循git fetch设置后,如果您使用分支或标记名称,则将得到响应,如果仅使用SHA-1,则不会得到响应。
这是我所做的:-从实际来源创建完整回购的完整工作克隆

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,作为一个新的仓库,所以我做了

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

这意味着我可以使用git --git-dir=../ui/.git format-patch -k -1 --stdout <sha1> | git am -3 -k从origin_point重建我的仓库git --git-dir=../ui/.git format-patch -k -1 --stdout <sha1> | git am -3 -k git --git-dir=../ui/.git format-patch -k -1 --stdout <sha1> | git am -3 -k远程选择樱桃,然后使用git push origin将整个批次上传回其新家。

希望可以帮助某人

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!