简体   繁体   中英

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). 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 . The constraint is up to the server, as described in the git upload-archive documentation :

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. However, because calculating object reachability is computationally expensive, git-upload-archive implements a stricter but easier-to-check set of rules:

  1. Clients may request a commit or tree that is pointed to directly by a ref. Eg, git archive --remote=origin v1.0 .

  2. Clients may request a sub-tree within a commit or tree using the ref:path syntax. Eg, git archive --remote=origin v1.0:Documentation .

  3. Clients may not use other sha1 expressions, even if the end result is reachable. Eg, neither a relative commit like master^ nor a literal sha1 like abcd1234 is allowed, even if the result is reachable from the refs.

Note that rule 3 disallows many cases that do not have any privacy implications. 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.

If the config option uploadArchive.allowUnreachable is true, these rules are ignored, and clients may use arbitrary sha1 expressions. 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.

For this same security reason, you cannot in general clone or fetch by hash ID in modern versions of Git. Site like GitHub that allow direct access to a commit by hash ID are doing so by working "around" Git, rather than through 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). This was new in Git 2.5. See Retrieve specific commit from a remote Git repository . Again, this must be enabled on the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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