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

如何克隆具备特定修订版的git存储库,就像我一般在Mercurial中所作的那样: html

hg clone -r 3 /path/to/repository

#1楼

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

再次回到最近的提交 git

$ git pull

#2楼

您能够简单地使用git checkout <commit hash> bash

按此顺序 gitlab

bash git clone [URLTORepository] git checkout [commithash] fetch

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


#3楼

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

好吧,它的2014年,以及查尔斯·贝利(Charles Bailey)从2010年开始接受的答案,如今看来已经彻底过期了,其余大多数(全部?)答案都涉及克隆,这是许多人但愿避免的。 url

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

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

# 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将整个批次上传回其新家。

但愿能够帮助某人

相关文章
相关标签/搜索