简体   繁体   English

我将如何使用git克隆回以前的远程版本?

[英]How would i clone back to a previous remote version with git?

I'm following a tutorial for learning rails (and I'm still kind of new to git). 我正在关注一个学习Rails的教程(并且我仍然是git的新手)。 I know how to revert back to a local version 我知道如何还原回本地版本

>>git log -p
>>git revert <sha1> //the sha1 to return to

Lets say I don't have local access anymore to the file but I have remote access to the repository, how would I clone into a specific version? 可以说我不再具有对该文件的本地访问权,但是可以远程访问该存储库,我该如何克隆到特定版本中?

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

The easiest way is to clone the whole repository, then check out the specific revision of interest. 最简单的方法是克隆整个存储库,然后签出感兴趣的特定修订版。 With large repositories, that may be expensive. 对于大型存储库,这可能会很昂贵。 You can sometimes reduce the cost by doing a "shallow" clone, but you will need to know how deep to go and whether there is a reference that is "close to" your desired hash. 有时您可以通过执行“浅”克隆来降低成本,但是您将需要知道要深入的程度以及是否有一个“接近”所需散列的引用。

You may or may not be allowed to retrieve one specific commit, as a tarball or zip archive, through git archive --remote . 您可能会或可能不会被允许通过git archive --remote来检索一个特定的提交,例如tarball或zip存档。 The constraint is up to the server, as described in the git upload-archive documentation : 约束由服务器决定,如git upload-archive文档中所述

SECURITY 安全

In order to protect the privacy of objects that have been removed from history but may not yet have been pruned, git-upload-archive avoids serving archives for commits and trees that are not reachable from the repository's refs. 为了保护已从历史记录中删除但尚未被修剪的对象的私密性,git-upload-archive避免为无法从存储库的引用访问的提交和树提供归档。 However, because calculating object reachability is computationally expensive, git-upload-archive implements a stricter but easier-to-check set of rules: 但是,由于计算对象的可到达性在计算上非常昂贵,因此git-upload-archive实现了一套更严格但更易于检查的规则:

  1. Clients may request a commit or tree that is pointed to directly by a ref. 客户端可以请求引用直接指向的提交或树。 Eg, git archive --remote=origin v1.0 . 例如, git archive --remote=origin v1.0

  2. Clients may request a sub-tree within a commit or tree using the ref:path syntax. 客户端可以使用ref:path语法在提交或树中请求子树。 Eg, git archive --remote=origin v1.0:Documentation . 例如, git archive --remote=origin v1.0:Documentation

  3. Clients may not use other sha1 expressions, even if the end result is reachable. 即使最终结果可以达到,客户端也不能使用其他sha1表达式。 Eg, neither a relative commit like master^ nor a literal sha1 like abcd1234 is allowed, even if the result is reachable from the refs. 例如,即使结果可以从abcd1234也不允许像master^这样的相对提交,也不像abcd1234这样的文字sha1是允许的。

Note that rule 3 disallows many cases that do not have any privacy implications. 请注意,规则3禁止许多不涉及隐私的情况。 These rules are subject to change in future versions of git, and the server accessed by git archive --remote may or may not follow these exact rules. 这些规则在将来的git版本中可能会发生变化,并且git archive --remote访问的服务器可能会也可能不会遵循这些确切规则。

If the config option uploadArchive.allowUnreachable is true, these rules are ignored, and clients may use arbitrary sha1 expressions. 如果配置选项uploadArchive.allowUnreachable为true, uploadArchive.allowUnreachable忽略这些规则,并且客户端可以使用任意sha1表达式。 This is useful if you do not care about the privacy of unreachable objects, or if your object database is already publicly available for access via non-smart-http. 如果您不关心无法访问的对象的私密性,或者您的对象数据库已经公开可用于通过非smart-http访问,则此功能很有用。

For this same security reason, you cannot in general clone or fetch by hash ID in modern versions of Git. 出于同样的安全原因,在现代版本的Git中,您通常无法通过哈希ID进行克隆或获取。 Site like GitHub that allow direct access to a commit by hash ID are doing so by working "around" Git, rather than through Git. 像GitHub这样的站点允许通过哈希ID直接访问提交,这是通过“围绕” Git而不是通过 Git进行的。 But : 但是

Not mentioned in the quote above is uploadpack.allowReachableSHA1InWant , which if enabled restores the old Git 1.5 style ability to fetch by hash (computationally expensively, on the server). 上面的报价中没有提到的是uploadpack.allowReachableSHA1InWant如果启用 ,它将恢复旧的Git 1.5样式功能,以通过哈希(在服务器上昂贵地计算)来获取。 This was new in Git 2.5. 这是Git 2.5中的新功能。 See Retrieve specific commit from a remote Git repository . 请参阅从远程Git存储库检索特定的提交 Again, this must be enabled on the server. 同样,必须在服务器上启用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM