如何从其 zip 克隆 git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15681643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:51:55  来源:igfitidea点击:

How to clone git repository from its zip

gitgithubzipgit-clone

提问by Lesto

I'm trying to clone a remote repository on github, but it is big and my connection doesn't seem to be stable enough, so I can't clone it successfully.

我正在尝试在 github 上克隆一个远程存储库,但是它很大而且我的连接似乎不够稳定,所以我无法成功克隆它。

But I have successfully downloaded the .zip of the repository.

但是我已经成功下载了存储库的 .zip。

Is there a way to use this zip as it was created by git clone, so I can push, pull etc..?

有没有办法使用这个 zip,因为它是由 git clone 创建的,所以我可以推、拉等......?

采纳答案by fracHymanmac

A related post hereprovides the information needed to grab the .gitdirectory and simplify the answer that uml?uteprovided:

此处的相关帖子提供了获取.git目录和简化uml?ute提供的答案所需的信息:

  • Grab the .gitdirectory by cloning a bare repository

    $ mkdir repo
    $ git clone --bare http://github/user/repo repo
    
  • Make the .gitdirectory and move the cloned files

    $ mkdir repo/.git
    $ mv repo/* repo/.git
    
  • Unzip the repository

    $ unzip repo.zip
    
  • Re-initialize the repository

    $ cd repo
    $ git init
    
  • Verify you're sync'ed

    $ git pull
    
  • Reset the HEAD to clean up the status

    $ git reset HEAD
    
  • Here's the log for the repo ... repo location - http://github.com/udacity/fullstack-nanodegree-vm

    $ git log
    commit ebcbda650bc81d7f4856f5314a0689cea5b43086
    Merge: 574774b b5b787e
    Author: Karl Krueger <[email protected]>
    Date:   Tue Apr 7 11:39:54 2015 -0700`
    
            Merge pull request #3 from pmallory/sharedDirAlert
    
            Add a login alert to explain how to access Vagrant's shared directory
    
    commit b5b787efdb1ecec0c3c9c7f9c0fd4732f984fcb3
    Author: Philip Mallory <[email protected]>
    Date:   Mon Apr 6 15:40:32 2015 -0700`
    
           move the alert into the motd
    
    commit b8012f33c86b0d19fc4c2b972af092e88d00978f
    Author: Philip Mallory <[email protected]>
    Date:   Mon Apr 6 14:32:01 2015 -0700`
    
           Add a login alert to explain how to access Vagrant's shared directory
    
    commit 574774ba29ccd661154431d5600240f090440c37
    Author: Lorenzo Brown <[email protected]>
    Date:   Wed Mar 11 14:08:02 2015 -0700`
    
           Update pg_config.sh
    
           Added installs for Auth&Auth
    
    commit 88fc5537b1a0017a1d76af4587a22412473809a4
    Author: Lorenzo Brown <[email protected]>
    Date:   Wed Mar 4 13:00:25 2015 -0800`
    
           Update and rename vagrant to vagrant/catalog/README.txt
    
    commit f978cdc14c62b7295d8da1a95452faaa1bd108b8
    Author: Lorenzo Brown <[email protected]>
    Date:   Wed Feb 4 11:06:03 2015 -0800`
    
           Update Vagrantfile
    
           switched to port forwarding on 8080
    
    commit d6a3a26578ef3c6d01d28abca76d817938892c7f
    Author: Lorenzo Brown <[email protected]>
    Date:   Tue Feb 3 14:52:34 2015 -0800`
    
           Update Vagrantfile
    
           Added:
    
           config.vm.network "forwarded_port", guest: 80, host: 8080
           config.vm.network "forwarded_port", guest: 5000, host: 5000
    
           FSF uses these two ports for lessons 2 & 3 respectively.
    
    commit 752a79e408c7328ef7f1766d1b97bb468ffed90a
    Author: Mike Wales <[email protected]>
    Date:   Mon Feb 2 11:21:29 2015 -0800`
    
           Removed .vagrant directory
    
    commit 5af9d19adf9ab19b1d886f6cc78e556f864b42dd
    Author: Mike Wales <[email protected]>
    Date:   Mon Feb 2 11:16:45 2015 -0800`
    
           Initial commit.
    
  • .git通过克隆裸存储库获取目录

    $ mkdir repo
    $ git clone --bare http://github/user/repo repo
    
  • 创建.git目录并移动克隆的文件

    $ mkdir repo/.git
    $ mv repo/* repo/.git
    
  • 解压存储库

    $ unzip repo.zip
    
  • 重新初始化存储库

    $ cd repo
    $ git init
    
  • 验证您已同步

    $ git pull
    
  • 重置 HEAD 以清理状态

    $ git reset HEAD
    
  • 这是回购的日志......回购位置 - http://github.com/udacity/fullstack-nanodegree-vm

    $ git log
    commit ebcbda650bc81d7f4856f5314a0689cea5b43086
    Merge: 574774b b5b787e
    Author: Karl Krueger <[email protected]>
    Date:   Tue Apr 7 11:39:54 2015 -0700`
    
            Merge pull request #3 from pmallory/sharedDirAlert
    
            Add a login alert to explain how to access Vagrant's shared directory
    
    commit b5b787efdb1ecec0c3c9c7f9c0fd4732f984fcb3
    Author: Philip Mallory <[email protected]>
    Date:   Mon Apr 6 15:40:32 2015 -0700`
    
           move the alert into the motd
    
    commit b8012f33c86b0d19fc4c2b972af092e88d00978f
    Author: Philip Mallory <[email protected]>
    Date:   Mon Apr 6 14:32:01 2015 -0700`
    
           Add a login alert to explain how to access Vagrant's shared directory
    
    commit 574774ba29ccd661154431d5600240f090440c37
    Author: Lorenzo Brown <[email protected]>
    Date:   Wed Mar 11 14:08:02 2015 -0700`
    
           Update pg_config.sh
    
           Added installs for Auth&Auth
    
    commit 88fc5537b1a0017a1d76af4587a22412473809a4
    Author: Lorenzo Brown <[email protected]>
    Date:   Wed Mar 4 13:00:25 2015 -0800`
    
           Update and rename vagrant to vagrant/catalog/README.txt
    
    commit f978cdc14c62b7295d8da1a95452faaa1bd108b8
    Author: Lorenzo Brown <[email protected]>
    Date:   Wed Feb 4 11:06:03 2015 -0800`
    
           Update Vagrantfile
    
           switched to port forwarding on 8080
    
    commit d6a3a26578ef3c6d01d28abca76d817938892c7f
    Author: Lorenzo Brown <[email protected]>
    Date:   Tue Feb 3 14:52:34 2015 -0800`
    
           Update Vagrantfile
    
           Added:
    
           config.vm.network "forwarded_port", guest: 80, host: 8080
           config.vm.network "forwarded_port", guest: 5000, host: 5000
    
           FSF uses these two ports for lessons 2 & 3 respectively.
    
    commit 752a79e408c7328ef7f1766d1b97bb468ffed90a
    Author: Mike Wales <[email protected]>
    Date:   Mon Feb 2 11:21:29 2015 -0800`
    
           Removed .vagrant directory
    
    commit 5af9d19adf9ab19b1d886f6cc78e556f864b42dd
    Author: Mike Wales <[email protected]>
    Date:   Mon Feb 2 11:16:45 2015 -0800`
    
           Initial commit.
    

回答by uml?ute

If you have downloaded the repository (including the.gitdir), it's quite simple.

如果您已经下载了存储库(包括.git目录),那就很简单了。

  • unzip the repository

    $ unzip repo.zip
    
  • configure a remotein your repository that points to the clone URI

    $ cd repo
    $ git init
    $ git remote add origin https://github.com/user/repo.git
    
  • resync the repositories

    $ git pull
    
  • 解压缩存储库

    $ unzip repo.zip
    
  • remote在您的存储库中配置一个指向克隆 URI 的

    $ cd repo
    $ git init
    $ git remote add origin https://github.com/user/repo.git
    
  • 重新同步存储库

    $ git pull
    

In practice, it seems that the "zip" download from github does notcontain the .gitdirectory, so this doesn't help :-(

在实践中,似乎从 github 下载的“zip”包含.git目录,所以这没有帮助:-(

Probably the best bet you have is to do a clone on a machine that does have stable access, and then zip the .git directory and fetch that somehow....

您可能最好的选择是在具有稳定访问权限的机器上进行克隆,然后压缩 .git 目录并以某种方式获取....

回答by arctelix

While the accepted answer does the trick, this seems a bit more straight forward.

虽然接受的答案可以解决问题,但这似乎更直接。

unzip <repo>.zip
cd <repo>
git init
git add .
git remote add origin https://github.com/<user>/<repo>.git
git remote update
git checkout master

Just make sure to replace <user>& <repo>with your github user name and your repo name ;)

只需确保将<user>&替换<repo>为您的 github 用户名和存储库名称;)

回答by kostix

The only zip-likealternative to cloning is exchanging "bundles", but I'm afraid github does not offer creation/downloading of bundles.

克隆的唯一类似 zip 的替代方法是交换"bundles",但恐怕 github 不提供创建/下载包的功能。

A zip archive downloadable from github is just a snapshot of one particular commit of your repository history (usually the tip of a branch), and it doesn't contain any history — this facility is intended to automatically provide the users of your code base (not developers!) with a way to conveniently download a snapshot of the project's source code. Note that mere users and, say, downstream maintainers packaging your software for operating systems, do not usually clone whole histories but rather work with tarballs.

可从 github 下载的 zip 存档只是您的存储库历史记录(通常是分支的尖端)的一个特定提交的快照,并且它不包含任何历史记录 — 此工具旨在自动向您的代码库的用户提供(不是开发人员!),可以方便地下载项目源代码的快照。请注意,单纯的用户和,例如,为操作系统打包软件的下游维护者,通常不会克隆整个历史记录,而是使用 tarball。

In other words, downloading a zip archive works like running git archiveon the remote side and then passing you the resulting file.

换句话说,下载 zip 存档就像git archive在远程端运行,然后将生成的文件传递给您。

Also note that repositories hosted on github (and other Git hosting providers) are "bare", that is, they do not contain the ".git" subdirectory.

另请注意,托管在 github(和其他 Git 托管提供商)上的存储库是“裸”的,即它们不包含“.git”子目录。

In any case, seems like your only way to solve this is to find a fast and reliable link and do your initial download using it.

无论如何,解决此问题的唯一方法似乎是找到一个快速可靠的链接并使用它进行初始下载。

But note that things changeif you're okay with not having the full history. You can then use the so-called "shallow cloning", by passing the "--depth" command-line parameter to git clone.

但请注意,如果您没有完整的历史记录,事情会发生变化。然后,您可以通过将“--depth”命令行参数传递给git clone.

回答by awiebe

Initialization might not be the way to go depending on your use case. It will be especially tedious if there is a very big repository(mine was 16GB and Ineeded to do this), and in fact will blow away local refs+objectswhich is no good if your archive represents an archive for which a remote no longer exists.

根据您的用例,初始化可能不是可行的方法。如果有一个非常大的存储库(我的是 16GB 并且需要这样做),这将特别乏味,并且实际上会吹走本地refs+objects,如果您的存档代表一个远程不再存在的存档,这将是不好的。

You need the repository to be copied in two steps:

您需要分两步复制存储库:

  1. "Data Files" (HEAD Revision possibly unstaged)
  2. Indices and Objects ie. history+refs+objects :This records your baseline, deltas, branch pointers and tags.
  1. “数据文件”(HEAD 修订版可能未上演)
  2. 索引和对象,即。history+refs+objects :这会记录您的基线、增量、分支指针和标签。

The objective is to reduce how many object must be cloned from a remote or which cannot be recreated from a remote which no longer exists. Additionally you want your config to not conflict with the local config files of whoever initially created the repository

目标是减少必须从远程克隆或无法从不再存在的远程重新创建的对象的数量。此外,您希望您的配置不与最初创建存储库的人的本地配置文件冲突

You want to preserve the repository layout for objects and refswhich not longer exist in the remote or which you don't want to copy (e.g they're large image assets). For this reason you don't want to init and pull, especially if you don't have a remote anymore.

您希望为远程中不再存在或您不想复制的对象和引用保留存储库布局(例如,它们是大型图像资产)。出于这个原因,您不想初始化和拉取,尤其是当您不再有遥控器时。

Instead the repository is already in an acceptable state with refs and objects intact. The only problems may be if the remotes are no longer setup properly and your config may be set improperly.

相反,存储库已经处于可接受的状态,引用和对象完好无损。唯一的问题可能是遥控器不再正确设置并且您的配置可能设置不正确。

Run git config --local -lto verify that the commit identity is not set locally in the repository and change any keys which override your global settings in a way you don't want.

运行git config --local -l以验证提交标识未在存储库中本地设置,并以您不希望的方式更改覆盖全局设置的任何键。

Now that it's configured treat the repository now as if it were your own(because it is), git is designed to work distributed so once you alter any local config, it is effectively no different, than as if it were cloned. The only thing remaining is you have to insure your remotes are setup properly.

既然它已配置好,现在将存储库视为您自己的存储库(因为它是),git 被设计为分布式工作,因此一旦您更改任何本地配置,它实际上与克隆一样。剩下的唯一一件事是您必须确保正确设置遥控器。

If you do not have remote but wish to create one, create it on the remote server using git init --barethen add a remote as usual and push all refs git push --all. Making a repository bare means it will accept the first push without complaining about a diverged history.

如果您没有远程但希望创建一个,请使用在远程服务器上创建它,git init --bare然后像往常一样添加远程并推送所有 refs git push --all。使存储库裸露意味着它将接受第一次推送而不会抱怨历史分歧。

If you have an existing remote repository, add it as a remote and pull. The archive branches may be pointing to the wrong url depending how long ago the archive was made, if this is the case, use git remoteto assign them to new locations or remove any dead urls.

如果您有现有的远程存储库,请将其添加为远程存储库并拉取。存档分支可能指向错误的 url,具体取决于存档的制作时间,如果是这种情况,请使用git remote将它们分配到新位置或删除任何无效的 url。

Once remotes have been setup fetch and pull to get up to date. If there is a detached HEAD, checkout the desired branch. If the history of the archived repository has diverged from the remote, git will produce a merge conflict, resolve as normal, stashing changes if necessary.

设置遥控器后,获取并拉动以获取最新信息。如果有一个分离的 HEAD,检查所需的分支。如果存档存储库的历史记录与远程存储库不同,git 将产生合并冲突,正常解决,必要时隐藏更改。