# サブフォルダを新規リポジトリに分割する

Git リポジトリ内のフォルダを、全く新しいリポジトリに変更できます。

> \[!NOTE]
> これらの手順に従うには Git バージョン 2.22.0 以降が必要です。それ以外の場合 `git filter-repo` は機能しません。

リポジトリの新しいクローンを作成した場合でも、フォルダを別のリポジトリに分割したとき、Git の履歴や変更を失うことはありません。 ただし、元のリポジトリのブランチとタグは新しいリポジトリに含まれないことに注意してください。

1. <span class="platform-mac">\[ターミナル]</span><span class="platform-linux">\[ターミナル]</span><span class="platform-windows">\[Git Bash]</span> を開きます。

2. 現在のワーキングディレクトリを、新しいリポジトリを作成したい場所に変更します。

3. サブフォルダのあるリポジトリをクローンします。

   ```shell
   git clone https://github.com/USERNAME/REPOSITORY-NAME
   ```

4. ワーキングディレクトリをクローンしたリポジトリに変更します。

   ```shell
   cd REPOSITORY-NAME
   ```

5. リポジトリ内の残りのファイルからサブフォルダーを除外するには、[`git-filter-repo`](https://github.com/newren/git-filter-repo) をインストールした後、次の引数を指定して `git filter-repo` を実行します。

   * `FOLDER-NAME`: 別のリポジトリを作成するプロジェクト内のフォルダー。

   <div class="ghd-tool windows">

   > \[!TIP]
   > Windowsユーザーは、`/` を使用してフォルダーを区切る必要があります。

   </div>

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

   これで、リポジトリにはサブフォルダー内にあったファイルだけが含まれるようになります。

   特定のサブフォルダーを新しいリポジトリの新しいルート フォルダーにする場合は、次のコマンドを使用してください。

   ```shell
   $ 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 に[新しいリポジトリを作成します](/ja/repositories/creating-and-managing-repositories/creating-a-new-repository)。

7. GitHub の \[クイック セットアップ] ページにある新しいリポジトリの上部で、<svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-copy" aria-label="Copy to clipboard" role="img"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg> をクリックしてリモート リポジトリの URL をコピーします。

   ![リポジトリ内の \[クイック セットアップ\] ヘッダーのスクリーンショット。 リモート URL の横にある 2 つの四角形が重なったアイコンがオレンジ色の枠線で囲まれています。](/assets/images/help/repository/copy-remote-repository-url-quick-setup.png)

   > \[!TIP]
   > HTTPS と SSH の URL の違いについては、「[リモートリポジトリについて](/ja/get-started/git-basics/about-remote-repositories)」を参照してください。

8. コピーした自分のリポジトリ用の URL を使って新しいリモート名を追加します。 たとえば、`origin` や `upstream` の 2 つが一般的な選択肢です。

   ```shell
   git remote add origin https://github.com/USERNAME/REPOSITORY-NAME.git
   ```

9. 新しいリポジトリの名前を使い、リモート URL が追加されたことを確認します。

   ```shell
   $ 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 の新しいリポジトリにプッシュします。

    ```shell
    git push -u origin BRANCH-NAME
    ```