{"meta":{"title":"以编程方式导入存储库","intro":"你可以以编程方式将存储库导入到 GitHub。","product":"迁移","breadcrumbs":[{"href":"/zh/enterprise-cloud@latest/migrations","title":"迁移"},{"href":"/zh/enterprise-cloud@latest/migrations/overview","title":"概述"},{"href":"/zh/enterprise-cloud@latest/migrations/overview/programmatically-importing-repositories","title":"以编程方式导入存储库"}],"documentType":"article"},"body":"# 以编程方式导入存储库\n\n你可以以编程方式将存储库导入到 GitHub。\n\n## 关于以编程方式导入存储库\n\n在下文指南中，您可以了解如何通过编程方式将 Git 存储库的“源代码和历史记录”迁移运行到 GitHub。 根据存储库的存储位置，可使用不同选项。\n\n若要详细了解“源和历史记录”和其他类型的迁移，请参阅“[规划到 GitHub 的迁移](/zh/enterprise-cloud@latest/migrations/overview/planning-your-migration-to-github)”。\n\n术语“源存储库”是指要导入的存储库，“导入的存储库”是指要创建的新存储库。\n\n## 使用分支\n\n如果源代码存储库位于 GitHub 上，则您可能可以使用派生，而不必导入该存储库。 通过分支，可更改项目，而不会影响原始存储库（也称为“上游”存储库）。\n\n创建存储库分叉后，可以：\n\n* 从上游库获取更新以保持您的派生同步到最新版本\n* 通过从分叉创建拉取请求来回馈原始项目。有关详细信息，请参阅 [叉子](/zh/enterprise-cloud@latest/pull-requests/reference/forks)。\n\n可以使用 REST API 以编程方式创建存储库分支。 有关详细信息，请参阅“[分支的 REST API 终结点](/zh/enterprise-cloud@latest/rest/repos/forks)”。\n\n如果用例满足以下任一条件，则无法使用分支代替直接导入存储库。\n\n* 导入的存储库应归源存储库的同一用户或组织所有。\n* 一个用户或组织应可多次导入同一源存储库。\n\n## 使用存储库模板\n\n如果源代码存储库位于 GitHub 上，则你或许可以使用存储库模板。\n可以从现有存储库创建模板。 拥有模板存储库访问权限的任何人均可基于目录结构、分支和文件相同的模板创建新存储库。 有关更多详细信息，请参阅 [创建模板仓库](/zh/enterprise-cloud@latest/repositories/creating-and-managing-repositories/creating-a-template-repository)。\n\n要使用存储库模板，必须拥有作为模板的现有存储库的读取权限，或者必须有权创建模板。\n\n可以使用 REST API 从存储库模板以编程方式创建存储库。 有关详细信息，请参阅“[存储库的 REST API 终结点](/zh/enterprise-cloud@latest/rest/repos/repos)”。\n\n## 使用 GitHub Enterprise Importer\n\n如果源存储库托管在 、、Azure DevOps Services、Bitbucket Server 或 Bitbucket Data Center 上，则可以使用  导入该存储库。 有关详细信息，请参阅“[关于 GitHub Enterprise Importer](/zh/enterprise-cloud@latest/migrations/using-github-enterprise-importer/understanding-github-enterprise-importer/about-github-enterprise-importer)”。\n\n除了源和版本控制历史记录之外， GitHub Enterprise Importer 还会迁移问题、拉取请求、设置等。\n\n若要使用 GitHub Enterprise Importer，必须具有对源存储库的管理员访问权限。\n\n可以使用 GraphQL API 以编程方式导入存储库 GitHub Enterprise Importer 。\n\n## 使用 Git CLI\n\n如果源存储库为 Git 存储库，则可以从代码以编程方式调用 Git CLI。 可以使用 GitHub 的 REST API 以编程方式创建一个存储库，然后使用 `git clone` 和 `git push` 等命令将该存储库导入到 GitHub。\n\nGit CLI 的调用方式因代码语言而异。 例如，在 Node.js 中，可以使用 `child_process` 模块，在 Ruby 中，可以使用 `open3` 模块。 有关详细信息，请参阅 Node.js 文档中的[子进程](https://nodejs.org/api/child_process.html)或 [ruby/open3 存储库](https://github.com/ruby/open3)GitHub。\n\n要使用 Git CLI，必须拥有在托管应用程序的系统上安装 Git 的权限。 有关详细信息，请参阅 Git 文档中的[入门指南 - 安装 Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)。\n\n## 使用 GitHub CLI\n\n如果源存储库是 Git 存储库，则可以从代码中以编程方式调用 GitHub CLI 。 可使用 `gh repo create` 创建存储库。 有关详细信息，请参阅“[关于 GitHub CLI](/zh/enterprise-cloud@latest/github-cli/github-cli/about-github-cli)”。\n\n## 其他阅读材料\n\n* [存储库的 REST API 终结点](/zh/enterprise-cloud@latest/rest/repos/repos)"}