如何使用特定修订版本克隆Git仓库,类似于我通常在Mercurial中所做的:
hg clone -r 3 /path/to/repository
$ git clone $URL
$ cd $PROJECT_NAME
$ git reset --hard $SHA1
回到最近的提交,请执行以下操作:
$ git pull
为了将撤销的提交保存在线上(远程),你必须强制推送到 origin:
git push origin -f
更新2 自从Git 2.5.0版本以来,可以通过配置变量uploadpack.allowReachableSHA1InWant
在服务器端启用下述功能,这里是GitHub的功能请求和启用该功能的GitHub提交。请注意,一些Git服务器默认激活此选项,例如Bitbucket Server自版本5.5+开始启用。参见Stackexchange上的答案,了解如何激活配置选项的示例。
更新1 对于Git版本1.7 < v < 2.5
,请使用git clone和git reset,如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
git fetch origin <sha1>
不起作用;似乎需要传递一个具有名称的引用,如标签或分支名称。参见 http://kerneltrap.org/mailarchive/git/2009/1/13/4707444 - arturreset --hard
来将分支实例化到本地后,可以通过 git fetch origin <SHA1>
切换到任何我想要的修订版本。但我无法直接获取单个修订版本。而在 git 1.7 版本中,如@artur 所述,git fetch origin <SHA1>
不起作用,需要使用git checkout <SHA1>
后跟reset --hard
。 - Joe McMahon如果想在特定的分支或标签上仅克隆一个单独的特定提交,请使用以下命令:
git clone --depth=1 --branch NAME https://github.com/your/repo.git
很遗憾,“NAME”只能是分支名称或标签名称(不能是提交 SHA)。
省略--depth
标志以下载整个历史记录,然后检出该分支或标签:
git clone --branch NAME https://github.com/your/repo.git
这适用于最近版本的Git(我使用的是版本2.18.0
)。
--branch
选项用于分支和标签。 - Peter Kovac克隆git存储库会完整地克隆整个存储库:没有一种方法可以选择仅克隆一个修订版本。但是,一旦执行了git clone
,您可以通过执行checkout <rev>
来检出特定的修订版本。
git clone
会获取整个存储库,一旦你拥有它,就可以检出特定的版本。 - user113292git clone --single-branch ...
- morxagit checkout <commit hash>
。bash
git clone [URLTORepository]
git checkout [commit hash]
提交哈希看起来像这样 "45ef55ac20ce2389c9180658fdba35f4a663d204"。git checkout
使修订内容成为当前内容。如果你只是克隆,你将处于默认(或指定)分支的顶部,这通常不是你想要的。 - Ruslangit clone --depth [N],其中N是您想要的历史版本数。然而:
--depth
创建一个浅层克隆,其中历史记录被截断为指定数量的修订版本。浅层存储库有一些限制(您无法从中克隆或提取,也无法将其推送到其中,也无法从其中推送),但如果您只对具有悠久历史的大型项目的最近历史感兴趣,并且希望作为补丁发送修复,则足够。
简单总结一下(git 版本 1.7.2.1):
git clone
命令(获取到目前为止所有的东西 - 我知道,这不是想要的内容,我们正在接近答案)git checkout <sha1 rev>
到你想要的版本git reset --hard
git checkout -b master
master
的本地分支,并切换到该分支。 - LarsHgit reset --hard
呢?文档上说“重置索引和工作目录。丢弃自<commit>(默认为 HEAD,现在是 <sha1 rev>
)以来工作目录中已跟踪文件的任何更改。”但此时我们自克隆以来还没有进行过任何更改,那么这个命令的目的是什么?它会截断当前分支至 <sha1 rev>
吗? - LarsHTL;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
希望这个解决方案能再运行几年!:-)
无需下载整个历史记录,也不需要调用git init
:
git clone --depth=1 URL
git fetch --depth=1 origin SHA1
git checkout SHA1
git branch -D @{-1} # if you want to tidy up the fetched branch
这种方法有一个缺点,与CB Bailey的回答类似,你仍然会下载1个不必要的版本。但从技术上讲,它是一个git clone
(OP所需),并且不强制你下载某个分支的整个历史记录。
2.40.0
版本时这两种情况都可以正常工作。你能证明相反的情况吗? - Johannesfatal: Couldn't find remote ref faf2a5a6
。所以我点赞你的方法,它很棒。 - WiringHarnessgit 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 clone --branch ...
的相反,它只接受SHA,而不适用于分支名称或标签。这样可以避免像我这样的人通过试错来找出问题,所以你知道这对什么有用。 - Tom Saleeba
git clone -b 10.1 https://github.com/MariaDB/server.git --depth=1 mariadb-server-src
- MrMeseesgit clone
命令,只下载那个提交。 - fonini