跳至主要内容

使用命令行导入外部 Git 仓库

如果你的 Git 仓库托管在不对公共互联网开放的代码托管服务上,你可以使用命令行上的 Git 将其导入。

开始之前,请确保你了解以下信息

  • 你的 GitHub 用户名
  • 外部仓库的克隆 URL,例如 https://external-host.com/user/repo.gitgit://external-host.com/user/repo.git(可能在 external-host.com 域名前面带有 user@

注意

为了演示,我们将使用

  • 一个名为 extuser 的外部账户
  • 一个名为 https://external-host.com 的外部 Git 主机
  • 一个名为 ghuser 的 GitHub 个人账户
  • GitHub.com 上名为 repo.git 的仓库
  1. 在 GitHub 上创建一个新仓库。你将把外部 Git 仓库导入到此新仓库中。

  2. 在命令行中,使用外部克隆 URL 对外部仓库执行“裸”克隆(bare clone)。这会创建数据的完整副本,但不包含用于编辑文件的工作目录,从而确保对所有旧数据进行干净、全新的导出。

    $ git clone --bare https://external-host.com/EXTUSER/REPO.git
    # Makes a bare clone of the external repository in a local directory
    
  3. 使用 “mirror” 选项将本地克隆的仓库推送到 GitHub,这可确保所有引用(例如分支和标签)都被复制到导入的仓库中。

    $ cd REPO.git
    $ git push --mirror https://github.com/USER/REPO.git
    # Pushes the mirror to the new repository on GitHub.com
    
  4. 删除临时的本地仓库。

    cd ..
    rm -rf REPO.git
    

如果你正在导入的仓库包含大型文件,可能会遇到警告或错误。有关大型文件及其管理的更多信息,请参阅 关于 GitHub 上的大文件

延伸阅读

© . This site is unofficial and not affiliated with GitHub, Inc.