跳至主要内容

将子文件夹拆分成新仓库

您可以将 Git 仓库中的文件夹转换为全新的仓库。

平台导航

注意

您需要 Git 2.22.0 或更高版本才能遵循这些指令,否则 git filter-repo 将无法工作。

如果您创建仓库的新克隆,当您将文件夹拆分为单独仓库时,Git 历史记录或更改都不会丢失。但请注意,新仓库不会拥有原仓库的分支和标签。

  1. 打开 终端终端Git Bash

  2. 将当前工作目录切换到您想创建新仓库的位置。

  3. 克隆包含子文件夹的仓库。

    git clone https://github.com/USERNAME/REPOSITORY-NAME
    
  4. 将当前工作目录切换到您克隆的仓库。

    cd REPOSITORY-NAME
    
  5. 要从仓库的其他文件中筛选出子文件夹,请安装 git-filter-repo,然后使用以下参数运行 git filter-repo

    • FOLDER-NAME:您希望在项目中创建单独仓库的文件夹。

    提示

    Windows 用户应使用 / 来分隔文件夹。

    $ git filter-repo --path FOLDER-NAME/
    # Filter the specified branch in your directory and remove empty commits
    

    仓库现在应该只包含您子文件夹中的文件。

    如果您想让某个特定子文件夹成为新仓库的根文件夹,可以使用以下命令

    $ git filter-repo --subdirectory-filter FOLDER-NAME
    # Filter the specific branch by using a single sub-directory as the root for the new repository
    
  6. 创建一个新仓库 在 GitHub 上。

  7. 在 GitHub 快速设置页面新仓库的顶部,单击复制远程仓库 URL。

    Screenshot of the "Quick Setup" header in a repository. Next to the remote URL, an icon of two overlapping squares is outlined in orange.

    提示

    有关 HTTPS 与 SSH URL 差异的信息,请参阅 关于远程仓库

  8. 使用您为仓库复制的 URL 添加一个新的远程名称。例如,originupstream 是常见的两种选择。

    git remote add origin https://github.com/USERNAME/REPOSITORY-NAME.git
    
  9. 验证已使用新仓库名称添加远程 URL。

    $ git remote -v
    # Verify new remote URL
    > origin  https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (fetch)
    > origin  https://github.com/USERNAME/NEW-REPOSITORY-NAME.git (push)
    
  10. 将您的更改推送到 GitHub 上的新仓库。

    git push -u origin BRANCH-NAME
    
© . This site is unofficial and not affiliated with GitHub, Inc.